1

i am having a issue with the Android SQLite Database. I want to set some default values, when i create the database with onCreate(). The database gets created but the table is empty.

//Datenbank anlegen
private static final String CREATETABLE_SETTINGS =
        "CREATE TABLE " + TABLE_SETTINGS + " (\n" +
                "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n" +
                "level TEXT DEFAULT 'hard',\n" +
                "design TEXT DEFAULT 'dark',\n" +
                "sound INTEGER DEFAULT '1',\n" +
                "music INTEGER DEFAULT '1',\n" +
                "vibration INTEGER DEFAULT '1',\n" +
                "control INTEGER DEFAULT '1');" ;

I don't know why the default values aren't working.

Thanks for every answer and excuse my bad language.

Nick
  • 135
  • 1
  • 4
  • 13
  • Post your code to set the default values.. – Psypher Jan 26 '15 at 17:25
  • And post any logcat/stacktrace when you are creating the database on device/emulator. – Morrison Chang Jan 26 '15 at 17:26
  • 2
    The code you have posted only creates the table itself. It does not insert any data into the table. – EJK Jan 26 '15 at 17:26
  • Firstly remove the '\n' after every column, second you have given String as input to Integer fields so its not getting inserted. Correct query : "CREATE TABLE " + TABLE_SETTINGS + " (" + "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + "level TEXT DEFAULT 'hard'," + "design TEXT DEFAULT 'dark'," + "sound INTEGER DEFAULT 1," + "music INTEGER DEFAULT 1," + "vibration INTEGER DEFAULT 1," + "control INTEGER DEFAULT 1)" – Psypher Jan 26 '15 at 17:46

1 Answers1

2

DEFAULT in a column definition is only applied if no or a null value is supplied for a column when inserting or updating rows. The code you posted does not insert any rows.

To insert a row with default values, consider

"INSERT INTO " + TABLE_SETTINGS + "(id) VALUES(NULL)"

(You need to specify at least one column when inserting.)

laalto
  • 150,114
  • 66
  • 286
  • 303
  • This was the solution. Didn't insert something. Thought at least one column would be added automatically. @Ranjith: Your tip was right to. – Nick Jan 26 '15 at 18:52