1

我对 sqlite 数据库有问题。我想用现有记录在 android 中创建数据库。我创建数据库:

public class PrzyslowiaArabskie  {

private static final String TABLE_NAME = "tabela_przyslowia";
private static final String DATABASE_NAME = "przyslowiadb";
private static final int DATABASE_VERSION = 1;
public static final String KEY_ID = "id";
public static final String KEY_PRZYSLOWIE = "przyslowie";

private DbHelper myHelper;
private final Context myContext;
private SQLiteDatabase myDatabase;




private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
         db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + 
                   KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                 KEY_PRZYSLOWIE + " TEXT NOT NULL);" );


    } 

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

        onCreate(db);
    }

}
public PrzyslowiaArabskie(Context c) {
    myContext = c;
}
public PrzyslowiaArabskie open() {
    myHelper = new DbHelper(myContext);
    myDatabase = myHelper.getWritableDatabase();
    return this;
}
public void close() {
    myHelper.close();
}
public long createEntry(String przyslowie) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_PRZYSLOWIE, przyslowie);
    return myDatabase.insert(TABLE_NAME, null, cv);

}
public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[] { KEY_ID, KEY_PRZYSLOWIE };
    Cursor c = myDatabase.query(TABLE_NAME, columns, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndexOrThrow(KEY_ID);
    int iPrzyslowie = c.getColumnIndexOrThrow(KEY_PRZYSLOWIE);


    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        result = result + c.getString(iRow) + " " + c.getString(iPrzyslowie) + "\n";
    }

    return result;
}

}

如果我想用现有记录创建数据库,我该怎么办。如何将记录与创建数据库一起添加?

4

3 回答 3

2

You can make a file containing all the insert statements needed to insert the data and put that file in your assets folder. After you create the database, then take each line of the file and run it through rawQuery.

There are other ways as well, as the other answer demonstrates.

于 2012-10-26T18:17:21.710 回答
2

1)如果您已经有数据,使用firefox的sqlite manager插入数据会更容易。

2)如果您有其他形式的数据,例如“CSV”或“XML”,那么您可以使用 Firefox 的 sqlite 管理器将该文件简单地导入您的表中。

3)或者如果您必须从您的应用程序插入数据,那么最好创建一个 bean 对象将其发送到您的数据库助手类并使用任何查询将值插入到您的表中。

举个例子,

    void addStatesModel(StatesModel states) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, states.getName()); // StatesModel Name
    values.put(KEY_CODE, states.getCode()); // StatesModel Phone

    // Inserting Row
    db.insert(TABLE_STATES, null, values);
    db.close(); // Closing database connection
   }

在此,我将状态作为 abean 对象传递给方法 addStatesModel。

4)或者,如果您在单独的数据库中有数据,您可以连接两个数据库,然后运行复制命令将数据从一个表传输到另一个表。或者只需使用 SQLite 管理器,您可以将表从一个数据库导出到 csv 文件或 xml文件,然后将该文件导入您的表中。

于 2012-10-26T18:18:38.637 回答
0

You just need to insert values in the onCreate() method, after you've created the table:

@Override
public void onCreate(SQLiteDatabase db) {
  db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
      + KEY_PRZYSLOWIE + " TEXT NOT NULL);" );

  ContentValues values = new ContentValues();
  values.put(KEY_PRZYSLOWIE , "My text");

  db.insert(TABLE_NAME, null, values);
  // ... etc...
} 

As a side note, if your database is more complicated than this simple example, you might want to delegate table creation and initialization to other methods or even classes.

于 2012-10-26T18:16:14.053 回答