0

****Exception 1****

E/SQLiteLog: (1) no such table: student_table
E/SQLiteDatabase: Error inserting NAME=yu DESIGNATION=hg SALARY=hgf
android.database.sqlite.SQLiteException: no such table: student_table 
(code 1): , while compiling: INSERT INTO student_table(NAME,DESIGNATION,SALARY) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)at 
com.example.admin.databaseapps.Databasehelp.Insertdata(Databasehelp.java:49)
at com.example.admin.databaseapps.MainActivity$2.onClick(MainActivity.java:140)
                      at android.view.View.performClick(View.java:5198)
                      at android.view.View$PerformClick.run(View.java:21147)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
V/RenderScript: 0xad9dc000 Launching thread(s), CPUs 2
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa29c7af0
E/SQLiteLog: (1) no such table: student_table
D/AndroidRuntime: Shutting down VM

Exception 2

  --------- beginning of crash

    E/AndroidRuntime: FATAL EXCEPTION: main    
                      Process: com.example.admin.databaseapps, PID: 2454
                      android.database.sqlite.SQLiteException: no such table:   student_table (code 1): , while compiling: select * from student_table
                          at 
    android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native 
    Method)
at 
android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
 at    android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
  at  android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
 at android.database.sqlite.SQLiteProgram.<init>
(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery.<init>
(SQLiteQuery.java:37)  at 
     android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.example.admin.databaseapps.Databasehelp.getAlldata(Databasehelp.java:60)
at com.example.admin.databaseapps.MainActivity$1.onClick(MainActivity.java:76)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Source Code:

public class Databasehelp extends SQLiteOpenHelper {

    public static final String Database_w = "Employ.db";

    public static final String Table_name = "student_table";

    public static final String col1 = "_ID";

    public static final String col2 = "NAME";

    public static final String col3 = "DESIGNATION";

    public static final String col4 = "SALARY";

    public static int database_version = 1;


    public Databasehelp(Context context) {

        super(context, Database_w, null, database_version);

    }

    @Override

    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        sqLiteDatabase.execSQL("CREATE TABLE " + Table_name + " (_ID INTEGER 

PRIMARY KEY AUTOINCREMENT,NAME TEXT,DESIGNATION TEXT,SALARY INTEGER)");

    }

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

        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + Table_name);


        onCreate(sqLiteDatabase);

    }

    public boolean Insertdata(String name, String Desig, String Sal) {

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();


  contentValues.put(col2, name);

        contentValues.put(col3, Desig);

        contentValues.put(col4, Sal);

       long result = sqLiteDatabase.insert(Table_name, null, contentValues);

        if (result == -1)

            return false;

        else

            return true;


    }

    public Cursor getAlldata() {


SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();



      Cursor res = sqLiteDatabase.rawQuery("select * from "+Table_name,null);


        return res;
    }
}
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
noman baig
  • 11
  • 1

1 Answers1

0

Im not sure how you instantiate your Databasehelp, but just to make sure, confirm that you do it this way:

   Databasehelp db = new Databasehelp(context);
    db.Insertdata(...);

If this is not the issue, then check what nikka wrote you in comments. A good way to debug is to sout as many things as possible; it'll show you where you went wrong.

Also, check this extension: http://facebook.github.io/stetho/

It will let you check the database structure using your browser, so you can make sure that your tables are created; you can also test your queries there and check for possible typos etc.

Vladimir Marton
  • 569
  • 4
  • 31