简体   繁体   English

SQLITE 新表 android

[英]SQLITE NEW TABLE android

I'm trying to add new tables to my database.我正在尝试将新表添加到我的数据库中。
I changed the database version, but it's not working - still have the same tables.我更改了数据库版本,但它不起作用 - 仍然有相同的表。

Here is the SQLiteOpenHelper class这是 SQLiteOpenHelper 类

public class DB extends SQLiteOpenHelper {

public final static String DBNAME="MEDCINEDB.db";
public final static String Table_name="MEDCINETable";
public final static String Table_name2="PPLTABLE";
public final static String col1="MEDCINEID";
public final static String col2="MEDCINENAME";
public final static String col3="MEDCINEPURPOSE";
public final static String col4="NOTAPLET";
public final static String col1T2="ID";
public final static String col2T2="NAMEPPL";
public final static String col3T2="AGEPPL";
public final static int DBVersion =2;


public DB(Context context,String DBNAME, int DBVersion) {
    super(context, DBNAME, null, DBVersion);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + Table_name + "(MEDCINEID INTEGER PRIMARY KEY AUTOINCREMENT,MEDCINENAME TEXT,MEDCINEPURPOSE TEXT,NOTAPLET INTEGER)");
    db.execSQL("CREATE TABLE " + Table_name2 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAMEPPL TEXT,AGEPPL INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP IF EXISTS"+Table_name);
    db.execSQL("DROP IF EXISTS"+Table_name2);
    onCreate(db);

}}

You need to call getWritableDatabase() or getReadableDatabase() to actually trigger the SQLiteOpenHelper lifecycle callbacks.您需要调用getWritableDatabase()getReadableDatabase()来实际触发SQLiteOpenHelper生命周期回调。

Then you'll also see an exception about the whitespace problem mentioned by "Frank".然后你还会看到一个关于“弗兰克”提到的空白问题的例外。

This is because you are not deleting yor existing tables:这是因为您没有删除现有的表:

db.execSQL("DROP IF EXISTS"+Table_name);
db.execSQL("DROP IF EXISTS"+Table_name2);

Should really be真的应该

db.execSQL("DROP TABLE IF EXISTS "+Table_name);
db.execSQL("DROP TABLE IF EXISTS "+Table_name2);

You were missing the TABLE keyword and a space between the EXISTS clause and the table names.您缺少TABLE关键字以及EXISTS子句和表名之间的空格


And the Database Version constant has to be named properly .并且必须正确命名数据库版本常量。

It's not它不是

public final static int DBVersion =2;

but

public final static int DATABASE_VERSION = 2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM