android sqlite创建表如果不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21838205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
android sqlite CREATE TABLE IF NOT EXISTS
提问by johnsonjp34
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);
}
}
回答by Gabe Sechan
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
.
这就是它应该如何工作。 CREATE TABLE
如果表已经存在,将抛出异常。 CREATE TABLE IF NOT EXISTS
如果表不存在,则将创建该表,如果存在则忽略该命令。如果要删除旧表,请使用DELETE TABLE IF EXISTS
before CREATE TABLE
。如果要更改架构,请使用ALTER TABLE
,而不是CREATE TABLE
。
回答by Роман Зыков
I use it like this:
我像这样使用它:
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; }
}