Android sqlite CREATE TABLE IF NOT EXISTING

Has a small problem with creating new tables. When I use the CREATE TABLE command, my new tables are formed as they should, but when I exit the action, the application crashes and I get TABLE ALREADY EXISTING in logcat. If I use CREATE TABLE, IF IT DOES NOT EXIST, a new table is not formed, but simply adds my new rows of data to the previous table, and this is not a failure. What is wrong with this? I would like to add a table without giving me an existing one, and I would like to add it without adding rows to another table.

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); } } 
+8
android sql sqlite create-table
source share
2 answers

How it should work. CREATE TABLE will throw an exception if the table already exists. CREATE TABLE IF NOT EXISTS will create a table if it does not exist, or ignore this command if this happens. If you want 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 .

+28
source share

I use it as follows:

  internal static int checkTable() { DataTable dTable = new DataTable(); try { dbConn = new SQLiteConnection("Data Source=" + dbFileName + ";Version=3;"); dbConn.Open(); sqlCmd.Connection = dbConn; String makeTable = "CREATE TABLE IF NOT EXISTS responde( id INTEGER PRIMARY KEY AUTOINCREMENT, sid TEXT, ans TEXT, answe TEXT, questid TEXT, timestamp TEXT)"; sqlCmd.CommandText = makeTable; sqlCmd.ExecuteNonQuery(); return 1; } catch (SQLiteException) { Console.WriteLine("Error DB 100"); return 0; } catch (IndexOutOfRangeException) { Console.WriteLine("Error DB 101"); return 0; } catch (TypeInitializationException) { Console.WriteLine("Error DB 102"); return 0; } } 
-one
source share

All Articles