Want to use sqlite database for an android application
Hi,
I want to create a sqlite database for use in an Android application to s开发者_如何学编程tore data for offline use. When there is no internet connectivity my application will show some data from this sqlite DB. Any pointers, where I should begin with?
Thank you
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;
}
}
here is the article link for the database
http://www.vogella.de/articles/AndroidSQLite/article.html
you can create sqlite database dynamically or copy existing database to our workspace.
Refer this link: http://www.screaming-penguin.com/node/7742
精彩评论