0

I am trying to save data into a database, but it seems that the onCreate method is not run. Might be this might be another problem but the logcat

12-01 01:22:41.785 19724-19724/com.example.user.timetable_test E/SQLiteLog: (1) near "null": syntax error
12-01 01:22:41.795 19724-19724/com.example.user.timetable_test E/SQLiteDatabase: Error inserting _length=1 _name=Break
                                                                                 android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO null(_length,_name) VALUES (?,?)
                                                                                 #################################################################
                                                                                 Error Code : 1 (SQLITE_ERROR)
                                                                                 Caused By : SQL(query) error or missing database.
                                                                                    (near "null": syntax error (code 1): , while compiling: INSERT INTO null(_length,_name) VALUES (?,?))
                                                                                 #################################################################
                                                                                     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
                                                                                     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
                                                                                     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                                                                                     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                                     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1607)
                                                                                     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
                                                                                     at com.example.user.timetable_test.setup.TableDBHandler.addEntry(TableDBHandler.java:79)
                                                                                     at com.example.user.timetable_test.setup.SetTable$PlaceholderFragment$1.onClick(SetTable.java:227)
                                                                                     at android.view.View.performClick(View.java:5697)
                                                                                     at android.widget.TextView.performClick(TextView.java:10826)
                                                                                     at android.view.View$PerformClick.run(View.java:22526)
                                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                     at android.os.Looper.loop(Looper.java:158)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

The SQLite class is:

package com.example.user.timetable_test.setup;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.user.timetable_test.MiscData;


public class TableDBHandler extends SQLiteOpenHelper{

    private MiscData data = MiscData.getInstance();

    private static int      DATABASE_VERSION   = 1;
    private static String   DATABASE_NAME      = "Timetable.db";
    private static String[] TABLE_DAY          = new String[MiscData.getInstance().getDays()];
    private static String   COLUMN_SESSION_NUM = "_id";
    private static String   COLUMN_NAME        = "_name";
    private static String   COLUMN_LENGTH      = "_length";
    private SQLiteDatabase db;


    public TableDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory,
                          int version){

        super(context, DATABASE_NAME, factory, DATABASE_VERSION);

        db = getWritableDatabase();
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase){


        Log.i("SQL", "DB Created");
        for(int i = 0; i < data.getDays(); i++){

            TABLE_DAY[i] = data.getDay(i + data.getFirstDay());

        }

        for(int i = 0; i < data.getDays(); i++){
            String query = "CREATE TABLE " + TABLE_DAY[i] + "(" + COLUMN_SESSION_NUM +
                           " INTEGER PRIMARY KEY ," + COLUMN_NAME + " TEXT," +
                           COLUMN_LENGTH + " INTEGER " + ");";

            sqLiteDatabase.execSQL(query);
        }
    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1){

        Log.i("SQL", "DB updated");


        for(int j = 0; j < data.getDays(); j++){
            TABLE_DAY[j] = data.getDay(j + data.getFirstDay());
            sqLiteDatabase.execSQL("DROP TABLE EXISTS " + TABLE_DAY[j]);
        }
        onCreate(sqLiteDatabase);


    }


    public void addEntry(int day, String name, int length){

        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, name);
        values.put(COLUMN_LENGTH, length);

        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_DAY[day], null, values);
        db.close();

    }
}

I am using an array to create a dynamic number of tables.

The log message in the onCreate method doesn't show up in the logcat, I checked a lot.

Calling the addEntry method:

tableDBHandler.addEntry(page, sessionSpinners[i].getSelectedItem().toString(), Integer.parseInt(lengthText[i].getText().toString()));

If I changed the DATABASE_VERSION from 1 the onUpgrade method is called, but if the version was 1 I cannot see the message from onCreat, in the logcat.

Shouldn't this class create a .db file? Because after clicking the button, and running the method, I can't see any files in the app's folder.

7
  • I bet something that you create tableDBHandler with tableDBHandler = new TableDBHandler(...);. Commented Nov 30, 2016 at 22:44
  • @Tom already did so, the app is not crashing, but I a getting this error in the logcat Commented Nov 30, 2016 at 22:46
  • And is this the correct way to create that instance? Who calls onCreate? Commented Nov 30, 2016 at 22:46
  • @Tom I don't know, buy it seems that it is not called in the first place, the Log.i("SQL", "DB Created"); line doesn't seem to run Commented Nov 30, 2016 at 22:48
  • 1
    Check your query: INSERT INTO null(_length,_name) VALUES (?,?) Commented Dec 1, 2016 at 3:26

1 Answer 1

5

INSERT INTO null - the table name is null. It comes from the TABLE_DAY array you initialize in your SQLite helper onCreate(). But onCreate() is only invoked when the database file is created, not each time you open your database.

You could move the TABLE_DAY init from onCreate() to e.g. constructor.

I am using an array to create a dynamic number of tables.

If the TABLE_DAY array is really dynamic, you're in much more trouble. Consider redesigning your database schema so that the tables are static but the content can be dynamic.

Sign up to request clarification or add additional context in comments.

2 Comments

tbh, I think a "dynamic number of tables" is more concerning than a dynamic array. Tables == static, their data, less so.
The number of tables is on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.