sqllite 增删改查创建表的语法
创建表
db.execSQL("create table user(_id integer primary key autoincrement,number varchar(20),age varchar(2)) ");
打开储存的数据库
SQLiteDatabase db = SQLiteDatabase.openDatabase(
"/data/data/com.itheima.mobileguard/files/address.db", null,
SQLiteDatabase.OPEN_READONLY);
添加
db.execSQL("insert into user(name, age) values(?,?)", new Object[]{name, age});
更新id为1的数据
db.execSQL("update user set name = '" + name + "',age='"+age+"' where id = 1");
db).execSQL("update user set name=?, phone=? where id=?",
new Object[]{person.getName(), person.age()});
向数据库添加一条数据
db.execSQL("insert into userinfo(name,age) values(?,?)",new String[]{name,age});
查找
查找指定id
db.rawQuery("select * from user where id = 1", null);
查找任何一个id
cursor =db.rawQuery("select name, age from user where id=?",
new String[]{String.valueOf(id)});
if(cursor.getCount() > 0)
{
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++)
{
cursor.moveToPosition(i);
user.setK(cursor.getString(cursor.getColumnIndex("name")));
user.setS(cursor.getString(cursor.getColumnIndex("age")));
}
}
分页查找
Cursor cursor = db.rawQuery("select name,age from user limit ? offset ?", new String[]{String.valueOf(pagesize),
String.valueOf(pagesize*pagenumber)
});
分批加载数据
Cursor cursor = db.rawQuery("select name,age from user order by _id desc limit ? offset ?",
new String[]{String.valueOf(maxCount),//最多加载多少条String.valueOf(startIndex)//从哪个位置
});
---------------
删除
db.delete("user", "name=?", new String[] { name});
查询全部
Cursor cursor = db.query("user", new String[]{"name","age"}, null, null, null, null, null);
List<UserInfo> blackNumberInfos = new ArrayList<UserInfo>();
while(cursor.moveToNext()){
UserInfo info = new UserInfo();
String name = cursor.getString(0);
String age = cursor.getString(1);
info.setName(mode);
info.setAge(number);
list.add(info);
}
cursor.close();
db.close();
获取总条目数
Cursor cursor = db.rawQuery("select count(*) from user",null);
cursor.moveToNext();
int count = cursor.getInt(0);