lisoslaw
lisoslaw

Reputation: 37

Application crashes when attempting to list database entries

Everything was all right until I reinstalled app on the phone. I can post my codes if necessary. I have no idea what caused this problem. Here is the logcat:

04-19 19:31:48.799: E/AndroidRuntime(24963): FATAL EXCEPTION: main
04-19 19:31:48.799: E/AndroidRuntime(24963): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.classorganizer/com.example.classorganizer.Monday}: android.database.sqlite.SQLiteException: no such table: diaries: , while compiling: SELECT * FROM diaries
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.os.Looper.loop(Looper.java:130)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at java.lang.reflect.Method.invokeNative(Native Method)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at java.lang.reflect.Method.invoke(Method.java:507)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at dalvik.system.NativeStart.main(Native Method)
04-19 19:31:48.799: E/AndroidRuntime(24963): Caused by: android.database.sqlite.SQLiteException: no such table: diaries: , while compiling: SELECT * FROM diaries
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1501)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1380)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1334)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1416)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.cookbook.data.MyDB.getdiaries(MyDB.java:66)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.example.classorganizer.Monday$DiaryAdapter.getdata(Monday.java:86)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.example.classorganizer.Monday$DiaryAdapter.<init>(Monday.java:80)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at com.example.classorganizer.Monday.onCreate(Monday.java:68)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-19 19:31:48.799: E/AndroidRuntime(24963):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
04-19 19:31:48.799: E/AndroidRuntime(24963):    ... 11 more

Here is my MyDBhelper file:

package com.cookbook.data;





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


public class MyDBhelper extends SQLiteOpenHelper{


private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "
;
private SQLiteDatabase db;



// database initialization
public MyDBhelper(Context context, String name, CursorFactory factory,
                    int version) {
    super(context, name, factory, version);

}



@Override
public void onCreate(SQLiteDatabase db) {
    Log.v("MyDBhelper onCreate","Creating all the tables");



    try {
        this.db = db;
        db.execSQL(CREATE_TABLE);

        for(int i=1; i <= 48; i++) { insertdiary(db, "free",""); }

    }

    catch(SQLiteException ex) {
        Log.v("Create table exception", ex.getMessage());

    }

}


// Saves a diary entry to the database as name-value pairs in ContentValues instance
            // then passes the data to the SQLitedatabase instance to do an insert
            public long insertdiary(SQLiteDatabase db, String title, String content)
            {


                try{
                    ContentValues newTaskValue = new ContentValues();
                    newTaskValue.put(Constants.TITLE_NAME,  title);
                    newTaskValue.put(Constants.CONTENT_NAME, content);

                    return db.insert(Constants.TABLE_NAME,  null, newTaskValue);

                } catch(SQLiteException ex) {
                    Log.v("Insert into database exception caught",
                            ex.getMessage());
                    return -1;
                }

            }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
                        int newVersion) {
    Log.w("TaskDBAdapter", "Upgrading from version "+oldVersion
                            +" to "+newVersion
                            +", which will destroy all old data");
    db.execSQL("drop table if exists "+Constants.TABLE_NAME);
    onCreate(db);
}




}

And here is MyDB file:

package com.cookbook.data;


import com.example.classorganizer.Diary;

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

public class MyDB {
private static final String TABLE_NAME = null;
private static final String KEY_ID = null;
private SQLiteDatabase db;
private final Context context;
private final MyDBhelper dbhelper;



// Initializes MyDBHelper instance
public MyDB(Context c){

    context = c;
    dbhelper = new MyDBhelper(context, Constants.DATABASE_NAME, null,
                                        Constants.DATABASE_VERSION);

}


// Closes the database connection
public void close()
{
    db.close();

}

// Initializes a SQLiteDatabase instance using MyDBhelper
public void open() throws SQLiteException
{

    try {
        db = dbhelper.getWritableDatabase();
    } catch(SQLiteException ex) {
        Log.v("Open database exception caught", ex.getMessage());
        db = dbhelper.getReadableDatabase();
    }

}

// updates a diary entry (existing row)
    public boolean updateDiaryEntry(String title, long rowId)
    {

        ContentValues newValue = new ContentValues();
        newValue.put(Constants.TITLE_NAME, title);

        return db.update(Constants.TABLE_NAME, newValue, Constants.KEY_ID + "=" + rowId, null)>0;

    }

// Reads the diary entries from database, saves them in a Cursor class and returns it from the method
public Cursor getdiaries()
{
    Cursor c = db.query(Constants.TABLE_NAME, null, null,
                        null, null, null, null);
    return c;
}


}

And here is the file that outputs newly created rows in a list:

package com.example.classorganizer;


import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.cookbook.data.Constants;
import com.cookbook.data.MyDB;




public class Monday extends ListActivity {



private static final int MyMenu = 0;
MyDB dba;
DiaryAdapter myAdapter;

private class MyDiary{
    public MyDiary(String t, String c){
        title=t;
        content=c;

        ListView listView = new ListView(Monday.this);
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                new EditListItemDialog(view.getContext()).show();

                return true;


            }
        });


}

    public String title;
    public String content;

}
@Override
protected void onCreate(Bundle savedInstanceState) {
    dba = new MyDB(this);
    dba.open();
    setContentView(R.layout.fragment_monday);





    super.onCreate(savedInstanceState);
    myAdapter = new DiaryAdapter(this);
    this.setListAdapter(myAdapter);
}



private class DiaryAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private ArrayList<MyDiary> fragment_monday;
    public DiaryAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        fragment_monday = new ArrayList<MyDiary>();
        getdata();


    }

    public void getdata(){
        Cursor c = dba.getdiaries();
        startManagingCursor(c);
        if(c.moveToFirst()){
            do{
                String title =
                        c.getString(c.getColumnIndex(Constants.TITLE_NAME));
                String content =
                        c.getString(c.getColumnIndex(Constants.CONTENT_NAME));

                MyDiary temp = new MyDiary(title,content);
                fragment_monday.add(temp);
            } while(c.moveToNext());
        }

    }



    @Override
    public int getCount() {return fragment_monday.size();}
    public MyDiary getItem(int i) {return fragment_monday.get(i);}
    public long getItemId(int i) {return i;}
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        final ViewHolder holder;

        View v = arg1;
        if ((v == null) || (v.getTag() == null)) {
            v = mInflater.inflate(R.layout.diaryrow,  null);
            holder = new ViewHolder();
            holder.mTitle = (TextView)v.findViewById(R.id.name);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        holder.mdiary = getItem(arg0);
        holder.mTitle.setText(holder.mdiary.title);


        v.setTag(holder);

        return v;


    }

    public class ViewHolder {
        MyDiary mdiary;
        TextView mTitle;


    }

}




/** Called when the user clicks the Edit button */
public void visitDiary(View view) {
    Intent intent = new Intent(this, Diary.class);
    startActivity(intent);
}
/** Called when the user clicks the back button */
public void visitSchedule(View view) {
    Intent intent = new Intent(this, DisplayScheduleScreen.class);
    startActivity(intent);
}


}

Upvotes: 0

Views: 98

Answers (4)

Christian Cederquist
Christian Cederquist

Reputation: 513

Here's your problem:

no such table: diaries

The problem is most likely, since you said you reinstalled the app, that you haven't created the database or the table you're selecting from.

EDIT: Now you posted the database creation script, there's a syntax error:

private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "
;

You're leaving the parenthesis after Constants.TABLE_NAME open. After Constants.CONTENT_NAME, write: +" text not null)"

Upvotes: 0

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

The problem is this line:

Constants.CONTENT_NAME+" text not null, "

Remove the last comma and add the closing bracket:

Constants.CONTENT_NAME+" text not null)"

Upvotes: 1

laalto
laalto

Reputation: 152817

Your onCreate() has SQL syntax problems but you catch SQLiteException so the framework thinks table creation succeeded. You should not be catching exceptions in sqlite helper lifecycle methods. If there's a problem, it should be propagated to the caller and not silently ignored.

As for the syntax problems,

private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null, "
;

should be something like

private static final String CREATE_TABLE="create table "+
Constants.TABLE_NAME+" ("+
Constants.KEY_ID+" integer primary key autoincrement, "+
Constants.TITLE_NAME+" text not null, "+
Constants.CONTENT_NAME+" text not null)"
;

Upvotes: 0

Lucas S.
Lucas S.

Reputation: 702

Here is your problem: SQLiteException: no such table: diaries.

Check if the table has been created in the database before trying to SELECT from it.

Upvotes: 0

Related Questions