Android ListView详解(7)
return sqLiteDatabase.rawQuery(sql, new String[]{String.valueOf(start),String.valueOf(size)});
}
//返回表中总记录条数
public int getCount(){
String sql = "select count(*) from person";
Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
if(cursor.moveToNext()){
return cursor.getInt(0);
}
return 0;
}
}
5.由于用到了android的SQLite数据库,对数据库操作的工具类代码如下
Java代码
package com.lamp.service;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseOpenHelper extends SQLiteOpenHelper {
private static final String DBNAME = "android";
private static final int VERSION = 1;
public DataBaseOpenHelper(Context context) {
super(context, DBNAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String sql = "create table person (personid integer primary key autoincrement,name varchar(20),age integer)";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
String sql = "drop table if exists person";
sqLiteDatabase.execSQL(sql);
onCreate(sqLiteDatabase);
}
}
首先通过单元测试往数据库中添加若干条记录,然后运行项目看到记录以列表的形式显示
作者“坐如松,动如风”
相关新闻>>
- 发表评论
-
- 最新评论 更多>>