android sqlite如果不存在则创建表 [英] android sqlite CREATE TABLE IF NOT EXISTS

查看:93
本文介绍了android sqlite如果不存在则创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建新表时存在一些问题。当我使用CREATE TABLE命令时,新表将按应有的形式显示,但是当我退出活动时,应用程序崩溃,并且在logcat中得到了一个表已经存在。如果我使用CREATE TABLE IF NOT EXISTS不存在,则不会形成新表,而只是将我的新数据行添加到上一个表中,并且不会崩溃。这有什么问题?我想添加一个没有表的表,让我已经存在,并且想要添加它而不向其他表添加行。

Having a little problem with creating new tables. When I use the CREATE TABLE command my new tables form as they should, but when I exit the activity the app crashes and I get a TABLE ALREADY EXISTS in the logcat. If I use CREATE TABLE IF NOT EXISTS the new table isn't formed but just adds my new rows of data to a previous table and it doesn't crash. What is wrong with this? I would like to add the table without it giving me the already exists and I would like it to add without it adding rows to a different table.

SqliteHelper类:

SqliteHelper Class:

public class MySQLiteHelper extends SQLiteOpenHelper {


public static final String TABLE_NAME = MainActivity.NameName;

public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
public static final String COLUMN_LAT = "lat";
public static final String COLUMN_LONG = "long";
public static final String COLUMN_RADI = "radi";
private static final String DATABASE_NAME = "spraylogs.db";
private static final int DATABASE_VERSION = 1;



public static final String NEWTABLE = "CREATE TABLE  "
         + TABLE_COMMENTS + "(" + COLUMN_ID
          + " integer primary key autoincrement, " + COLUMN_COMMENT
          + " text not null," + COLUMN_LAT+ ","  + COLUMN_LONG + "," + COLUMN_RADI +  ");";

public static final String SaveIt = "CREATE TABLE IF NOT EXISTS "
         + TABLE_COMMENTS + "(" + COLUMN_ID
          + " integer primary key autoincrement, " + COLUMN_COMMENT
          + " text not null," + COLUMN_LAT+ ","  + COLUMN_LONG + "," + COLUMN_RADI +  ");";



 MySQLiteHelper(Context context) {
  super(context, context.getExternalFilesDir(null).getAbsolutePath() + "/" + DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase database) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
    "Upgrading database from version " + oldVersion + " to "
        + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + NEWTABLE);

onCreate(db);
 } 

} 


推荐答案

这就是它应该如何工作的。如果表已经存在,创建表将引发异常。 如果不存在则创建表将创建该表(如果不存在),或者忽略该命令(如果存在)。如果要删除旧表,请在创建表之前使用如果存在则删除表。如果要更改架构,请使用 ALTER TABLE ,而不要使用 CREATE TABLE

That's how it's supposed to work. CREATE TABLE will throw an exception if the table already exists. CREATE TABLE IF NOT EXISTS will create the table if it doesn't exist, or ignore the command if it does. If you want it to delete the old table, use DELETE TABLE IF EXISTS before CREATE TABLE. If you want to change the schema, use ALTER TABLE, not CREATE TABLE.

这篇关于android sqlite如果不存在则创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆