0

i am begining in android.

i create sqlite database in android sdk 2.2, but generate a fatal error on belove code help me..

first file

package com.and.database.demo;

import java.util.List;
import com.and.database.helper.MyHelperActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MyDatabaseActivity extends Activity {

private TextView text_out;
private MyHelperActivity dh;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    text_out = (TextView)findViewById(R.id.textout);
    this.dh = new MyHelperActivity(this);

    this.dh.deleteAll();

    this.dh.insert("durgesh");
    this.dh.insert("Mukesh");
    this.dh.insert("Sk");
    this.dh.insert("Givind");
    this.dh.insert("Parth");

    List<String> names = this.dh.SelectAll();
    StringBuilder sb = new StringBuilder();
    sb.append("Names in database:\n");

    for(String name : names)
    {
        sb.append(name + "\n");
    }

    Log.d("EXAMPLE", "names size - " + names.size());
    this.text_out.setText(sb.toString());
}

}

second file helper class.

package com.and.database.helper;

import java.util.ArrayList;
import java.util.List;

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

public class MyHelperActivity {

private static final String DATABASE_NAME = "mydata.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "mytable";

private Context context;
private SQLiteStatement insertstm;
private SQLiteDatabase db;

private static final String INSERT ="insert into " + TABLE_NAME + " (name) values(?)"; 

public MyHelperActivity(Context context) {
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
    this.insertstm = this.db.compileStatement(INSERT);

}

public long insert (String name){
    this.insertstm.bindString(1, name);
    return this.insertstm.executeInsert();

}

public void  deleteAll() {
    this.db.delete(TABLE_NAME , null, null);
}

public List<String> SelectAll(){
    List<String>list = new ArrayList<String>();
    Cursor cursor = this.db.query(TABLE_NAME, new String[]{"name"}, null, null, null, null,"name desc");
      if (cursor.moveToFirst()) {

          do {
              list.add(cursor.getString(0));
          }while(cursor.moveToNext());
      }
      if (cursor != null && cursor.isClosed()) {
             cursor.close();
          }

    return list;

}

private static class OpenHelper extends SQLiteOpenHelper {


    public OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }


    public void onCreate(SQLiteDatabase db) {

           db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, name TEXT)");

    }


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

        Log.w("Example", "Upgrading database, this will drop tables and recreate.");
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
         onCreate(db);
    }

}

}

following error generate belove

03-05 09:00:35.918: ERROR/AndroidRuntime(371): FATAL EXCEPTION: main
03-05 09:00:35.918: ERROR/AndroidRuntime(371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.and.database.demo/com.and.database.demo.MyDatabaseActivity}: android.database.sqlite.SQLiteException: no such table: mytable: , while compiling: insert into mytable (name) values (?);
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.os.Looper.loop(Looper.java:123)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at java.lang.reflect.Method.invoke(Method.java:521)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at dalvik.system.NativeStart.main(Native Method)
03-05 09:00:35.918: ERROR/AndroidRuntime(371): Caused by: android.database.sqlite.SQLiteException: no such table: mytable: , while compiling: insert into mytable (name) values (?);
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at com.and.database.helper.MyHelperActivity.<init>(MyHelperActivity.java:29)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at com.and.database.demo.MyDatabaseActivity.onCreate(MyDatabaseActivity.java:19)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-05 09:00:35.918: ERROR/AndroidRuntime(371):     ... 11 more 

1 Answer 1

1

Make sure the code in onCreate() of OpenHelper is being executed, looks like you did not create the table.

Also, add @Override on top of the onCreate, onUpgrade

@Override 
public void onCreate(SQLiteDatabase db) {

       db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, name TEXT)");

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

Comments

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.