views:

92

answers:

3

Hi, here is the error from the logcat of my program.

06-24 01:35:04.213: INFO/ActivityManager(587): Starting activity: Intent { comp={one.two/one.two.Arrival} } 06-24 01:35:04.521: ERROR/DBDroid(1048): android.database.sqlite.SQLiteException: no such table: port: , while compiling: SELECT KEY_STATUS, KEY_ID, KEY_ARRIVAL, KEY_DESTINATION, KEY_FERRY FROM ferry 06-24 01:35:05.026: INFO/ActivityManager(587): Displayed activity one.two/.Arrival: 805 ms

It seems that it is stated that there is no table called port. But i do have a table called port placed into the emulator , under data/data/one.two/databases

And here is my partial code of DBAdapter:

    public class DBAdapter extends ListActivity {
public static  String KEY_STATUS = "status";
public static  String KEY_ID = "id";
public static  String KEY_ARRIVAL = "arrival";
public static  String KEY_DESTINATION = "destination";
public static  String KEY_FERRY = "ferry";

private static final String DATABASE_NAME = "ferry";
private static final String DATABASE_TABLE = "port";

private static final int DATABASE_VERSION = 1;

 private static  Context context;
 private static DatabaseHelper DBHelper;
 private static SQLiteDatabase db;

By the way does anyone here knows what does this method do :

public DBAdapter(Context ctx)
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context)
    {
        super(context, "ferry", null, 1);
    }

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
    }
}//end DatabaseHelper

// ---opens the database---
public DBAdapter open() throws SQLException
{
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close()
{
    DBHelper.close();
}

Thank you.

A: 

Are you absolutely sure the table exists ?

Try to open database invoking following commands from shell (I guess DATABASE_NAME is the name of database file):

adb shell
sqlite3 /data/data/one.two/databases/ferry

SQLite console should open. And then list tables:

.tables

Use .quit to exit SQLite.

edit:

If your database file is ferry.db then DATABASE_NAME should also be "ferry.db"

Gawcio
Hi Gawcio, i followed your instructions and changed the database name to ferry.db , there was a port table in the adb shell.
User358218
I'd like you to post onCreate() method of DatabaseHelper class and query that causes exception.You can always try to delete database file allowing SQLiteHelper to create it again.
Gawcio
Ok Edited my codes.
User358218
+1  A: 

You should put table creation code in DatabaseHelper.onCreate() method - SQL CREATE statements for all tables you're going to use in other queries.

edit:

Unless you're providing ".db" file already created outside Android (e.g. using SQLite Database Browser tool - http://sqlitebrowser.sourceforge.net) within your application package (e.g. already populated with data). Then you don't have to put any code here (moreover if you put code that will try to create table that exists you'll get an SQL exception).

Gawcio
I don't have a table creation code, i created a db and edited the data using sqlbrowser and changed the table data there, would this make my code not work?
User358218
I didn't use the tool. But it might be possible...
Gawcio
So i would leave onCreate method blank?
User358218
Yeah, this ( empty onCreate() ) is not a problem in your use case.
Gawcio
A: 

I'm having a similar problem as you. With this error at logcat:

06-25 03:14:24.740: ERROR/Database(813): Error inserting title=New!  entry=New entry.  time=03 : 13 date=25 / 6 / 2010  mood=null

06-25 03:14:24.740: ERROR/Database(813): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 06-25 03:14:24.740: ERROR/Database(813): at android.database.sqlite.SQLiteStatement.native_execute(Native Method) 06-25 03:14:24.740: ERROR/Database(813): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:66) 06-25 03:14:24.740: ERROR/Database(813): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1313) 06-25 03:14:24.740: ERROR/Database(813): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1173) 06-25 03:14:24.740: ERROR/Database(813): at one.two.DBAdapter.insertEntry(DBAdapter.java:122) 06-25 03:14:24.740: ERROR/Database(813): at one.two.New_Entry$5.onClick(New_Entry.java:197) 06-25 03:14:24.740: ERROR/Database(813): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 06-25 03:14:24.740: ERROR/Database(813): at android.os.Handler.dispatchMessage(Handler.java:99) 06-25 03:14:24.740: ERROR/Database(813): at android.os.Looper.loop(Looper.java:123) 06-25 03:14:24.740: ERROR/Database(813): at android.app.ActivityThread.main(ActivityThread.java:3948) 06-25 03:14:24.740: ERROR/Database(813): at java.lang.reflect.Method.invokeNative(Native Method) 06-25 03:14:24.740: ERROR/Database(813): at java.lang.reflect.Method.invoke(Method.java:521) 06-25 03:14:24.740: ERROR/Database(813): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782) 06-25 03:14:24.740: ERROR/Database(813): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 06-25 03:14:24.740: ERROR/Database(813): at dalvik.system.NativeStart.main(Native Method)

Whats the problem?

Here is the Database:

public class DBAdapter 

{
//values for the login table public static final String KEY_ROWID = "_id"; public static final String KEY_USER = "user"; public static final String KEY_PASSWORD = "pass"; public static final String KEY_NUMBER ="no";

//Values for the entry table
public static  String KEY_ROWID2 = "_id2";
public static  String KEY_TITLE = "title";
public static  String KEY_ENTRY = "entry";
public static  String KEY_MOOD = "mood";
public static  String KEY_DATE = "date";
public static  String KEY_TIME = "time";

private static final String TAG = "DBAdapter";
//declare Database name, tables names
private static final String DATABASE_NAME = "Database3";
private static final String DATABASE_TABLE = "Login";
private static final String DATABASE_TABLE_2 = "Entry";
private static final int DATABASE_VERSION = 1;

//declares the rules for the database tables
private static final String DATABASE_CREATE =
    "create table login (_id integer primary key autoincrement, "
    + "user text not null, pass text not null," 
    + " no integer not null);";

private static final String DATABASE_CREATE_2 =
    "create table entry (_id2 integer primary key autoincrement, "
    + "title text,entry text, mood text not null, date text not null, "
    + "time text not null);";


private final Context context; 

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    //Create the tables with the rules we set.
    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(DATABASE_CREATE);
        db.execSQL(DATABASE_CREATE_2);
    }

    //OnUpgrade is only for use when u changed the database's version to 2 etc.
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }
}    
UserA
It is much easier to read when it is formatted - select block and use 11001 icon above message edit area.
Gawcio
You're problem is "constraint failed". Without more info about db scheme and query I can't help you.
Gawcio
Take a look at line 122 of DBAdapter.java - there's some constraint violation - maybe _id column value is not unique or any other field has null value ...
Gawcio