public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "upgrade.db";
public static final String TITLE = "title";
public static final String AUTHOR = "author";
public static final String ISBN = "isbn";
public static final String CITY = "city";
public static final String CITY2 = "city2";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null,3);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( "CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT ,author TEXT,isbn TEXT,city TEXT );");
Log.v("onCreate-------","called onCreate");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
android.util.Log.w("books",
"Upgrading database, which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS books");
onCreate(db);
Log.v("onUpgrade-------","called onUpgrade with alter");
}
}
在其他课程中,您可以致电:-
public void onLoad(View v)
{
DatabaseHelper dbh = new DatabaseHelper(this);
SQLiteDatabase sl = dbh.getReadableDatabase();
Cursor c = sl.rawQuery("select * from books",null);
c.moveToFirst();
/* here your code to fetch data*/
Toast.makeText(this,"hello",Toast.LENGTH_SHORT).show();
sl.close();
}