作者:砖头资源 | 来源:互联网 | 2023-05-16 09:19
Havingalittleproblemwithcreatingnewtables.WhenIusetheCREATETABLEcommandmynewtables
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.
创建新表时遇到一些问题。当我使用CREATE TABLE命令时,我的新表格应该如此形成,但是当我退出活动时,应用程序崩溃,我在logcat中获得了一个TABLE ALREADY EXISTS。如果我使用CREATE TABLE IF NOT EXISTS,则不会形成新表,只是将新的数据行添加到上一个表中,并且不会崩溃。这有什么问题?我想添加表没有它给我已经存在,我希望它添加没有它添加行到不同的表。
SqliteHelper Class:
SqliteHelper类:
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);
}
}
2 个解决方案