0

I have the following table configuration

    package com.lynas.entertainmenttracker;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBConnectionTableMovie {
    public static final String DBNAME = "entertainment_tracker";
    public static final String DBTABLE = "movie";
    public static final int VERSION = 1;

    public static final String DBCREATE_MOVIE = "CREATE TABLE movie(" +
            "movie_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "movie_name VARCHAR, " +
            "movie_release_date date, " +
            "movie_extra VARCHAR)";

    private final Context context;
    private DBHelper dbh;
    private SQLiteDatabase db;

    public DBConnectionTableMovie(Context ctx){

        this.context = ctx;
        dbh = new DBHelper(context);
    }


    private static class DBHelper extends SQLiteOpenHelper{

        DBHelper(Context context){
            super(context, DBNAME,null,VERSION);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {
            //try{
                db.execSQL(DBCREATE_MOVIE);
            //}catch(SQLException e){
                //e.printStackTrace();
            //}

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE movie IF EXISTS");
            onCreate(db);

        }

    }
    public DBConnectionTableMovie open() throws SQLException{
        db = dbh.getWritableDatabase();
        return this;
    }

    public void close() throws SQLException{
        dbh.close();
    }

    public void addANewRow(String inserQuery){
        db.execSQL(inserQuery);
    }
    public void updateRow(String updateQuery){
        db.execSQL(updateQuery);
    }
    public void deleteRow(String deleteQuery){
        db.execSQL(deleteQuery);
    }
    public Cursor selectRow(String selectQuery){
        return db.rawQuery(selectQuery, null);
    }


}

I am not sure if this is relevant or not but i used same database name for another table public static final String DBNAME = "entertainment_tracker";

I am trying to insert a row using following query by creating an object of that class. its in an activity thus used the this as constructor

DBConnectionTableMovie dbcon = new DBConnectionTableMovie(this);
String insertANewMovie = "INSERT INTO movie VALUES (NULL, 'name', '2013-05-01', 'empty')";

dbcon.open();
dbcon.addANewRow(insertANewMovie);
dbcon.close();

I am getting table not exist error. Can someone tell me whats wrong?

error log

05-14 00:24:18.366: E/Database(856): Failure 1 (no such table: movie) on 0x259138 when preparing 'INSERT INTO movie VALUES (NULL, 'jack', '2013-05-14', 'empty')'.

LynAs
  • 6,407
  • 14
  • 48
  • 83
  • 1
    i think "INSERT INTO movie VALUES (NULL, 'name', '2013-05-01', 'empty')"; is sufficient – G_S May 14 '13 at 00:23
  • I think @G_S is right. Try removing the quotes around 'movie'. – Don May 14 '13 at 00:24
  • did does getting same error – LynAs May 14 '13 at 00:25
  • Try removing the try/catch block when creating the table in your DBHelper class. It's good to have that when your app is ready to be deployed, but I leave it out until then so that the app will crash if the table creation is not successful. – Don May 14 '13 at 00:29
  • once check onCreate() of your DBConnectionTableMovie class. Check if you are getting any exception – G_S May 14 '13 at 00:30
  • 1
    Also, you can replace "create table if not exists" with "create table". SQLiteOpenHelper will only create the table once. – Don May 14 '13 at 00:31
  • yes @Don is right.. Try replacing it – G_S May 14 '13 at 00:33
  • tried everything above this comment still same problem. no such table: movie – LynAs May 14 '13 at 00:33
  • Can you update the code in your question please? – Don May 14 '13 at 00:34
  • 1
    My immediate guess with the error is either a typo or you need to clear data to update the changes since the tables wont be recreated otherwise. – Andy May 14 '13 at 00:35
  • code updated, please check – LynAs May 14 '13 at 00:36
  • Once check whether the database is created in your data/data/myproject/ path via ddms view. If yes delete the database and run the project again – G_S May 14 '13 at 00:36
  • sorry coppied wrong table, check now – LynAs May 14 '13 at 00:37
  • Once check whether the database is created in your data/data/myproject/ path via ddms view. If yes delete the database file and run the project again. Tried this? – G_S May 14 '13 at 00:45
  • added some more description at the end of the table please check – LynAs May 14 '13 at 00:47
  • @LynAs Alright, here's what I'd recommend. Clear all of the data on the phone (if you're running in the emulator restart it and select the option to clear the snapshot and remove saved data). It's likely that something was messed up from before. After that, run the code again and check to see if you're getting any exceptions when you open the database and create the table for the first time. – Don May 14 '13 at 00:50
  • I dont think its an issue and better follow @Don – G_S May 14 '13 at 00:50
  • 1
    @LynAs SQLite Allows you to create multiple tables per database. Here's your problem: **Somehow, the `movie` table is not being created.** In addition to what I mentioned above, try querying sqlite_master for a list of current tables right before your INSERT statement and print out the results from the sqlite_master query. See this link for more info on how to query sqlite_master: http://stackoverflow.com/a/82889/1852466 – Don May 14 '13 at 00:59
  • I should note, sqlite_master contains a list of internal tables. Don't worry about those. Just see if the `movie` table exists. – Don May 14 '13 at 01:03

0 Answers0