开发者

SQLite3 db Android inserting nulls

String TABLE_NAME = "WORD";
public void onCreate(SQLiteDatabase db) {

         db.execSQL("CREATE TABLE " + TABLE_NAME + 
          "(WORD TEXT, DEFINITION TEXT, DAY TEXT)");
         //find out if we have entered data already
             ContentValues cv = new ContentValues();
             cv.put("WORD", "nuttin");
             cv.put("DEFINITION", "nuttin");
             cv.put("DAY", "nuttin");
             db.insert("WORD", null, cv );

             ContentValues cvUpdates = new ContentValues();
        开发者_如何学编程     cvUpdates.put("WORD", "   ");
             cvUpdates.put("DEFINITION", "   ");
             cvUpdates.put("DAY", "   ");
             db.update("WORD", cvUpdates, null, null);


      }

Why do I keep getting

sqlite> select * from word;
||

beating my head against the wall on this one...


There is a problem with the SQL-string you're building to create your tables:

"CREATE TABLE " + TABLE_NAME + "(WORD TEXT, DEFINITION TEXT, DAY TEXT)"

This will create:

CREATE TABLE WORD(WORD TEXT, DEFINITION TEXT, DAY TEXT)

You can see that it's missing a white-space between the table-name and the fields (... WORD( ...).

If you have a look at your LogCat output, you might notice that the execSQL()-method throws an SQLException telling you that the SQL-String is invalid. You should always catch those Exceptions!

Just for the sake of completeness: The SQLException-class extends RuntimeException, which is an unchecked exception. More on this can be found here.


It was actually because, almost directly after the DB was created, I was using a null HashMap to insert some values into it instead of populating the HashMap with the return of my method (which actually worked.. I just forgot to use it) Doh! Thanks guys.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜