您好,
我想创建一个sqlite数据库,用于在Android应用程序中存储数据以供离线使用。当没有互联网连接时,我的应用程序将显示来自此sqlite DB的一些数据。任何指针,我应该从哪里开始?
谢谢
答案 0 :(得分:3)
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "DATABASE_NAME", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE yourTable ("
+ "id INTEGER PRIMARY KEY, "
+ "name TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int o, int arg2) {
db.execSQL("DROP TABLE IF EXISTS yourTable");
onCreate(db);
}
public void insertMethod(String name) {
SQLiteDatabase db = getWritableDatabase();
String insertQuery = "INSERT INTO yourTable (name) VALUES(?)";
SQLiteStatement stmt = db.compileStatement(insertQuery);
stmt.bindString(1, name);
stmt.execute();
stmt.close();
db.close();
}
public String getMethod() {
String name;
SQLiteDatabase db = getReadableDatabase();
String selectQuery = "SELECT name FROM yourTable LIMIT 1";
Cursor cur = db.rawQuery(selectQuery, null);
cur.moveToFirst();
if(cur.isAfterLast() == false)
name= cur.getString(0);
cur.close();
db.close();
return name;
}
}
答案 1 :(得分:1)
答案 2 :(得分:0)
您可以动态创建sqlite数据库或将现有数据库复制到我们的工作区。