【问题标题】:How to add second table with one database in sql ite in android,data not inserted in second table如何在 android 的 sql ite 中使用一个数据库添加第二个表,数据未插入到第二个表中
【发布时间】:2015-11-16 10:55:37
【问题描述】:

这是我的代码。

 public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        String create="create table "+TABLE_NAME+"("+FIELD1+"  integer primary key  autoincrement, "+FIELD2+" Text, "+FIELD3+" Text )";
        String createone="create table "+TABLE_USERS+"("+KEY_FNAME+" Text, "+KEY_LNAME+" Text)";  
        db.execSQL(create);
        db.execSQL(createone);

        public void add(String fnm,String lnm)
        {
         SQLiteDatabase dbone=this.getWritableDatabase();
         ContentValues cvone=new ContentValues();
         cvone.put(KEY_FNAME, fnm);
         cvone.put(KEY_LNAME, lnm);
         dbone.insert(TABLE_USERS, null,cvone);
        }    

【问题讨论】:

  • 添加第二条CREATE TABLE 语句。然后增加DATABASE_VERSION 的值。
  • 当你想添加新表时..??
  • 请将代码发送到 add.with Oncreate method.i 也可以,但没有成功

标签: android


【解决方案1】:

我有这样的.. 根据您的要求更改字段。

public void createTable (String tableName) {

final SQLiteDatabase db = getWritableDatabase();
String TABLE_NEW_CONVERSATION = tableName;

String CREATE_TABLE_NEW_CONVERSATION = "CREATE TABLE " + TABLE_NEW_CONVERSATION +
        " (" + CONVERSATION_ID + " INTEGER PRIMARY KEY," +
        CONVERSATION_MESSAGE + " TEXT," +
        CONVERSATION_ISDELIVERED + " TEXT," +
        CONVERSATION_TIME + " TEXT," +
        CONVERSATION_DATE + " TEXT," +
        CONVERSATION_PATH + " TEXT," +
        CONVERSATION_FROM + " TEXT )";

db.execSQL(CREATE_TABLE_NEW_CONVERSATION);
db.close();

}

【讨论】:

  • 必须关闭数据库吗?
  • 如何从设备获取 .db 文件..??
  • 我们在 Eclipse IDE 中工作。所以我们使用 SQLite DB Browser.For 显示表数据。
  • 您也可以通过进入 Eclipse 中的 DDMS 透视图(Window menu\ Open perspective \ Other可能位于 \data\data*Your Application Name*\databases。左上角有一个“拉文件”按钮,如下所示:
  • 我知道..!!现在你怎么称呼这个函数..??
【解决方案2】:

要将另一个表添加到已经存在的数据库中,请修改您的 onUpgrade 方法。 `onUpgrade 在数据库需要升级时调用;请注意,您必须增加 VERSION_NUMBER(您希望将其作为私有实例变量包含在您的类中)

@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
    db.executeSQL(DATABASE_CREATE2);
}

现在 DATABASE_CREATE2 是

private static final String DATABASE_CREATE1 = "create table IF NOT EXISTS "
+ TABLE_CHAPTER + "( " + COLUMN_ID
+ " integer primary key autoincrement, " 
+ COLUMN_SUBJECT + " text not null, "
+ COLUMN_CHAPTER + " text, "
+ COLUMN_QUESTION + " text not null,"
+ COLUMN_OPTIONA + " text not null,"
+ COLUMN_OPTIONB + " text not null,"
+ COLUMN_OPTIONC + " text not null,"
+ COLUMN_OPTIOND + " text not null,"
+ COLUMN_CORRECT + " text not null,"
+ COLUMN_CONFIRM + " text not null);";

【讨论】:

  • 另一种方法是什么意思?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 2021-11-18
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
  • 2019-06-09
相关资源
最近更新 更多