问题是我不知道如何只初始化整个表一次.
我做了很多阅读,我发现你可以做到
在helper类中重写onCreate(sqliteDatabase db)方法.
这些是我的数据字段和我的onCreate(sqliteDatabase db)方法
public static final String KEY_ROWID = "_ID"; public static final String KEY_name = "name"; public static final String KEY_EIGHTU = "8u"; public static final String KEY_NINEU = "9u"; public static final String KEY_TENU = "10u"; public static final String KEY_ELEVENU = "11u"; private static final String TAG = "DBAdapter"; @OverrIDe public voID onCreate(sqliteDatabase db) { db.execsql(DATABASE_CREATE); }
但我不确定如何…任何想法?
我的主要问题是我有多行我想初始化
解决方法 试试这种方式:public class MyDBHelper { // Database propertIEs private static final String DATABASE_name = "Test.sqlite"; private static final String DATABASE_table_name = "table 1"; private static final int DATABASE_VERSION = 4; // table1 propertIEs public static final String KEY_ROWID = "_ID"; public static final String KEY_name = "name"; public static final String KEY_EIGHTU = "8u"; public static final String KEY_NINEU = "9u"; public static final String KEY_TENU = "10u"; public static final String KEY_ELEVENU = "11u"; private static final String TAG = "DBAdapter"; // Create Script private static final String DATABASE_CREATE_PLAYER = "CREATE table IF NOT EXISTS " + DATABASE_table_name + "( " + KEY_ROWID + " INTEGER," + KEY_name + " TEXT," + KEY_EIGHTU + " TEXT," + KEY_NINEU + " TEXT," + KEY_TENU + " TEXT," + KEY_ELEVENU + " TEXT);"; private final Context m_context; private DatabaseHelper m_dbHelper; public static sqliteDatabase m_db; public MyDBHelper(Context ctx) { this.m_context = ctx; m_dbHelper = new DatabaseHelper(m_context); } private static class DatabaseHelper extends sqliteOpenHelper { DatabaseHelper(Context context) { super(context,DATABASE_name,null,DATABASE_VERSION); } @OverrIDe public voID onCreate(sqliteDatabase db) { db.execsql(DATABASE_table_name); } @OverrIDe public voID onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { db.execsql("DROP table IF EXISTS " + DATABASE_table_name ); onCreate(db); } } //---opens the database--- public MyDBHelper open() throws sqlException { m_db = m_dbHelper.getWritableDatabase(); return this; } //---closes the database--- public voID close() { if (m_db != null) m_db.close(); if (m_dbHelper != null) m_dbHelper.close(); } }总结
以上是内存溢出为你收集整理的初始化一个sqlite数据库android全部内容,希望文章能够帮你解决初始化一个sqlite数据库android所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)