asc 升序
desc 降序
_id integer primary key autoincrement (id 整型 主键 自增长)
Integer类型主键 会自动自增长,可以不加autoincrement
SQL分页显示?
① selete * from testtable limit 2,1;
② selete * from testtable limit 2 offset 1;
这两个都是能完成需要,但是他们之间是有区别的:
①是从数据库中第三条开始查询,取一条数据,即第三条数据
②是从数据库中的第二条数据开始查询两条数据,即第二条和第三条。
增删改 execSQl();
查rawQuery();
Cursor cursor=db.rawQuery (“select *....”) 将查询结果存在结果游标集里
SQLiteDatabase Demo
Class extends DatabaseOpenHelper
DBhelper helper=new DBhelper(context);
SQLiteDatabase db=null ;
db=helper .getReadable()/getWirteable
Try{
查询方法: Cursor cursor=db.rawQuery()
}finally{
If(db!=null)
db.close();
}
While(cursor.movetoNext)
New一个实体类list对象存放信息
String name=cursor.getString(cursor.getcolmnIndex(name));
通过cursor获取name所在列
String pwd。。。。。。。。。
//写一个类,类中写入static方法,用于调用
public class DBManager {
//保存
public static void saveInfo(Context context,ScoreInfo item){
DBOpenHelper helper=new DBOpenHelper(context);
SQLiteDatabase db=null;
try {
db= helper.getWritableDatabase();
db.execSQL("insert into info ('name','score') values (?,?)", new Object[]{item.getName(),item.getScore()});
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(db!=null)
db.close();
}
}
public static List<Info> getInfoList(Context context,int diff){
List<Info> list=new ArrayList<Info>();
DBOpenHelper helper=new DBOpenHelper(context);
SQLiteDatabase db=null;
try {
db= helper.getWritableDatabase();
Cursor cursor=db.rawQuery("select name,score from info where____ order by score asc", new String[]{});
int i=1;
while(cursor.moveToNext()){
Info item=new Info();
item.setId(i++);
//通过cursor中方法用列名获取所处的列数
item.setName(cursor.getString(cursor.getColumnIndex("name")));
item.setScore(cursor.getInt(cursor.getColumnIndex("score")));
list.add(item);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(db!=null)
db.close();
}
return list;
}
}