*
数据库使用测试 * *
admin * class sqliteTestActivity
extends Activity
implements OnClickListener { private Button addBtn,addListBtn,delBtn,updateBtn,queryBtn,countBtn,pagingBtn,otherBtn; private TextView text; private DatabaseHelper databaseHelper; @Override void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { addBtn =
(Button) findViewById(R.id.add); addListBtn =
(Button) findViewById(R.id.addList); delBtn =
(Button) findViewById(R.id.delete); updateBtn =
(Button) findViewById(R.id.update); queryBtn =
(Button) findViewById(R.id.query); countBtn =
(Button) findViewById(R.id.count); pagingBtn =
(Button) findViewById(R.id.paging); otherBtn =
(Button) findViewById(R.id.other); 设置监听
addBtn.setOnClickListener(
this); addListBtn.setOnClickListener(this); delBtn.setOnClickListener(this); updateBtn.setOnClickListener(this); queryBtn.setOnClickListener(this); countBtn.setOnClickListener(this); pagingBtn.setOnClickListener(this); otherBtn.setOnClickListener(this); text =
(TextView) findViewById(R.id.text); databaseHelper =
new DatabaseHelper(
this); } @Override void onClick(View v) { * 添加对象 */
if (v.equals(addBtn)) { Person person =
new Person(); person.setName("Eric"
); person.setAge(23
); addData(person); } * 添加对象集合 if (v.equals(addListBtn)) { ArrayList<Person> personList =
new ArrayList<Person>
(); Person person =
new Person(); person.setName("Tom"
); person.setAge(20
); personList.add(person); Person person1 =
new Person(); person1.setName("Jack"
); person1.setAge(21
); personList.add(person1); Person person2 =
new Person(); person2.setName("Harvey"
); person2.setAge(22
); personList.add(person2); addData(personList); } * 删除数据 if (v.equals(delBtn)) { deleteData(1
); } * 更新数据 if (v.equals(updateBtn)) { Person person =
new Person(); person.setId(2
); person.setName("Bob"
); person.setAge(35
); updateData(person); } * 查询数据 if (v.equals(queryBtn)) { queryData(3
); } * 数据总数 if (v.equals(countBtn)) { System.out.println("
查询总数=====" +
countData()); } * 分页 if (v.equals(pagingBtn)) { getScrollData(0,3
); } if (v.equals(otherBtn)) { other(); } } void addData(Person person) { sqliteDatabase db = databaseHelper.getWritableDatabase();
创建或者打开一个可写数据库 插入数据
db.exec
sql("INSERT INTO person(name,age) VALUES(?,?)",255); font-size:12px!important; line-height:1.5!important">new
Object[] { person.getName(),person.getAge() }); Log.i("
sqliteTestActivity","name:" + person.getName() + "\nage:" +
person.getAge()); } * 添加对象集合 * * personList void addData(ArrayList<Person>
personList) { sqliteDatabase db = databaseHelper.getWritableDatabase();
创建或者打开一个可写数据库
db.beginTransaction();
开始事务
try { for (Person person : personList) { db.execsql("INSERT INTO person(name,"name:" + person.getName() + "\nage:" +
person.getAge()); } db.setTransactionSuccessful(); 设置事务成功完成
} finally { db.endTransaction(); 结束事务
} } * 删除数据 * * id void deleteData(Integer id) { sqliteDatabase db = databaseHelper.getWritableDatabase();
创建或者打开一个可写数据库
db.exec
sql("delete from person where personid=?",255); font-size:12px!important; line-height:1.5!important">new
Object[] { id }); } void updateData(Person person) { sqliteDatabase db =
databaseHelper.getWritableDatabase(); db.execsql("update person set name=?,age=? where personid=?",person.getAge(),person.getId() }); }
void queryData(Integer id) { sqliteDatabase db = databaseHelper.getReadableDatabase();
创建或者打开一个查询数据库
Cursor cursor = db.rawQuery("select * from person where personid=?",255); font-size:12px!important; line-height:1.5!important">new
String[] { String.valueOf(id) }); 迭代记录集
if (cursor.moveToNext()) { Person person =
new Person(); person.setId(cursor.getInt(cursor.getColumnIndex("personid"
))); person.setName(cursor.getString(cursor.getColumnIndex("name"
))); person.setAge(cursor.getInt(cursor.getColumnIndex("age"
))); 将查到的字段,放入person
System.out.println(person.toString()); text.setText(person.toString()); } cursor.close(); 游标关闭
} * 获取记录总数 * * @return
long countData() { sqliteDatabase db =
databaseHelper.getReadableDatabase(); 没有占位符参数的话,直接用null
Cursor cursor = db.rawQuery("select * from person",255); font-size:12px!important; line-height:1.5!important">null
); int count =
cursor.getCount(); cursor.close(); return count; } * 分页 * * offset * count void getScrollData(
int offset,255); font-size:12px!important; line-height:1.5!important">int
count) { ArrayList<Person> persons =
new ArrayList<Person>
(); sqliteDatabase db =
databaseHelper.getReadableDatabase(); offset开始索引 count 记录条数
Cursor cursor = db.rawQuery("select personid,age from person limit ?,?",255); font-size:12px!important; line-height:1.5!important">new
String[] { String.valueOf(offset),String.valueOf(count) }); while (cursor.moveToNext()) { Person person =
new Person(); person.setId(cursor.getInt(cursor.getColumnIndex("personid"
))); person.setName(cursor.getString(cursor.getColumnIndex("name"
))); person.setAge(cursor.getInt(cursor.getColumnIndex("age"
))); persons.add(person); Log.i("
sqliteTestActivity","name:" + person.getName() + "\nage:" +
person.getAge()); } System.out.println("大小================" +
persons.size()); cursor.close(); } void other() { Intent intent =
new Intent(
sqliteTestActivity.
this,OtherActivity.
class); startActivity(intent); } }
OtherActivity.java(第二种方法)
class OtherActivity
private DatabaseHelper databaseHelper; private ArrayList<Person>
personList; @Override super.onCreate(savedInstanceState); setContentView(R.layout.other); init(); } void init() { addBtn =
(Button) findViewById(R.id.add); addListBtn =
(Button) findViewById(R.id.addList); delBtn =
(Button) findViewById(R.id.delete); updateBtn =
(Button) findViewById(R.id.update); queryBtn =
(Button) findViewById(R.id.query); countBtn =
(Button) findViewById(R.id.count); pagingBtn =
(Button) findViewById(R.id.paging); 设置监听
addBtn.setOnClickListener(
new Person(); person.setName("Eric"
); person.setAge(3
); addData(person); } if (v.equals(addListBtn)) { personList =
new Person(); person.setName("Tom"
); person.setAge(2
); personList.add(person); Person person1 =
new Person(); person1.setName("Jack"
); person1.setAge(3
); personList.add(person1); Person person2 =
new Person(); person2.setName("Harvey"
); person2.setAge(6
); personList.add(person2); addData(personList); } new Person(); person.setId(3
); person.setName("Bob"
); person.setAge(0
); updateData(person); } if (v.equals(countBtn)) { System.out.println("
查询个数=====" +
countData()); } ); } } * 添加数据 创建或者打开一个可写数据库
ContentValues contentValues =
new ContentValues(); contentValues.put("name"
,person.getName()); contentValues.put("age"
,person.getAge()); db.insert("person",contentValues); }
* 添加集合数据 * * for (Person person : personList) { ContentValues contentValues =
sqliteTestActivity",0); font-size:12px!important; line-height:1.5!important"> 创建或者打开一个可写数据库
db.delete("person","personid=?",255); font-size:12px!important; line-height:1.5!important">new
String[] { String.valueOf(id) }); } void updateData(Person person) { ContentValues contentValues =
sqliteDatabase db =
databaseHelper.getWritableDatabase(); * 第一个参数表示表名 /第二个参数表示更新的数据/第三个参数表示sql语句的中条件部分的语句 /第四个参数占位符的值 */ db.update("person",contentValues,255); font-size:12px!important; line-height:1.5!important">new
String[] { String.valueOf(person.getId()) }); } 创建或者打开一个查询数据库
* 第一个参数表示表名 /第二个参数表示查找需要返回的字段/第三个参数表示sql语句的中条件部分的语句 * /第四个参数占位符的值/第五个参数表示分组 * 可设为null/第六个参数表示sql语句中的having,可设为null/第七个参数表示结果的排序,可设为null */ Cursor cursor = db.query("person",255); font-size:12px!important; line-height:1.5!important">new
String[] { "personid","name","age"
},"personid=?",255); font-size:12px!important; line-height:1.5!important">new
String[] { String.valueOf(id) },null,255); font-size:12px!important; line-height:1.5!important">new
Person(); person.setId(cursor.getInt(cursor.getColumnIndex("personid"
))); person.setName(cursor.getString(cursor.getColumnIndex("name"
))); person.setAge(cursor.getInt(cursor.getColumnIndex("age")));
long countData() { sqliteDatabase db =
databaseHelper.getReadableDatabase(); Cursor cursor = db.query("person",255); font-size:12px!important; line-height:1.5!important">new
String[] { "*"
},255); font-size:12px!important; line-height:1.5!important">int count =
cursor.getCount(); cursor.close(); 游标关闭
new ArrayList<Person>
(); sqliteDatabase db =
databaseHelper.getReadableDatabase(); Cursor cursor = db.query("person",offset + "," +
count); new Person(); person.setId(cursor.getInt(cursor.getColumnIndex("personid"
))); person.setName(cursor.getString(cursor.getColumnIndex("name"
))); person.setAge(cursor.getInt(cursor.getColumnIndex("age"
))); persons.add(person); Log.i("OtherActivity","name:" + person.getName() + "\nage:" +
person.getAge()); } System.out.println("大小================" +
persons.size()); cursor.close(); } }
原文链接:https://www.f2er.com/sqlite/199495.html