android sqlite CREATE TABLE IF NOT EXISTS

只愿长相守 提交于 2019-12-03 23:41:50
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.

Роман Зыков

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; }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!