简体   繁体   English

Android SQLite中的原始查询-插入或忽略

[英]Raw Query in Android SQLite - INSERT OR IGNORE

I'm having an issue inputting information into a Sqlite database on the app I'm creating. 我在创建的应用程序上将信息输入Sqlite数据库时遇到问题。 I was using the help of the Cursor before. 我以前是在游标的帮助下使用的。 I am used to MySQL although clearly not 'used to' that well. 我已经习惯了MySQL,尽管显然不能“很好地”使用。

I am trying to add to the database from a file. 我正在尝试从文件添加到数据库。 I had this working before but it would be added with the Cursor. 我以前曾做过这项工作,但它将与Cursor一起添加。 I was then told that in order to make it so I could add new information to the file and have the app ONLY add the new information into the database I should use INSERT OR IGNORE. 然后有人告诉我,为了做到这一点,我可以将新信息添加到文件中,并使应用程序仅将新信息添加到数据库中,所以我应该使用INSERT或IGNORE。

Is this the correct syntax? 这是正确的语法吗? I currently am not having any information inserted for whatever reason... 无论出于何种原因,我目前都没有插入任何信息...

ourDatabase.rawQuery("INSERT OR IGNORE INTO " + DATABASE_TABLE + " ("+KEY_CLASS+",
 " + KEY_QUESTION+ ") VALUES ('" + qclass + "', '" + question + "');", null);

This is my database: 这是我的数据库:

"CREATE TABLE " + DATABASE_TABLE + " (" + 
                    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                    KEY_CLASS + " TEXT NOT NULL, " +
                    KEY_QUESTION + " TEXT NOT NULL UNIQUE);

Thanks for the help in advance! 我在这里先向您的帮助表示感谢!

Your query seems right but try the one below anyway 您的查询似乎正确,但还是请尝试以下查询

ContentValues insertValues = new ContentValues();
insertValues.put(KEY_CLASS, qclass);
insertValues.put(KEY_QUESTION, question);   
yourDbName.insertWithOnConflict(DATABASE_TABLE, null, insertValues, SQLiteDatabase.CONFLICT_IGNORE);  

KEY_QUESTION + " TEXT NOT NULL UNIQUE);";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM