2011-03-05 63 views
0

如何在android中聲明數據庫?我們如何在android和方法中創建數據庫?Android SQLite及其方法

+0

的可能重複[使用SQLite Android]產品(http://stackoverflow.com/questions/3298898/android-with-sqlite) – 2011-03-05 04:31:40

回答

1

基本上你擴展類SQLiteOpenHelper和實施onCreateonUpgrade

public class DatabaseHelper extends SQLiteOpenHelper { 
    private static final String DATABASE_NAME = "db_table"; 
    public DatabaseHelper2(Context context) { 
     super(context, DATABASE_NAME, null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE coordinate (field1 CHAR NOT NULL ,field2 INTEGER NOT NULL ,field3 INTEGER NOT NULL);"); 
     db.execSQL("INSERT INTO spotit VALUES(test ,10,10);"); 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 

然後在您的主要活動中啓動該類。

DatabaseHelper db = new DatabaseHelper(this); 
    db.getReadableDatabase(); 
    db.close(); 

當你打電話的onCreate將被調用db.getReadableDatabase()