如何在android中的sql ite中添加第二个表和一个数据库,未在第二个表中插入数据

时间:2015-11-16 10:55:38

标签: android

这是我的代码。

 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);
        }    

2 个答案:

答案 0 :(得分: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();

}

答案 1 :(得分:0)

要将另一个表添加到现有数据库,请修改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);";