RuntimeError while retriving data from sqlite database in android

前端 未结 4 1685
情深已故
情深已故 2021-01-27 01:31

I\'m trying to insert and retrieve data from database in eclipse using sqlite, but it shows a RuntimeError. I create a layout with three edit texts and one button t

相关标签:
4条回答
  • 2021-01-27 01:48

    You need init ctxx in onCreate() method:

    ctxx = this;
    

    If you want to know more about sqlite on android -> ok. But if you want to less code you can try some SqliteLibrary for android. (example ActiveAndroid, GreenDAO.....).

    0 讨论(0)
  • 2021-01-27 01:49

    In your activity initialize the ctxx in onCreate:

    ctxx=this;

    or

    DBHelper DB=new DBHelper(MainActivity.this);

    whenever you will initialize database object will null you will get sqlite locked exception.

    0 讨论(0)
  • 2021-01-27 01:52

    Also fixe this

    public void onCreate(SQLiteDatabase db) {
            data=db;
            db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                    + NAME + " TEXT,"
                    + FATHER_NAME + " TEXT,"
                    + MOTHER_NAME + " TEXT"
                    + ");");
            Log.d("DATABASE OPERATION", "TABLE CREATED");
    
    
        }
    

    for "INTEGER," remove ,.

    and intialise it like this DBHelper.

    DBHelper DB=new DBHelper(MainActivity.this);
    
    0 讨论(0)
  • 2021-01-27 02:01

    ctxx is never initialzed, and this probably the cause of the crash. Generally speaking, when you deal with Activity and Fragment subclass, you almost never need to keep a reference to the Context. Activity is a subclass of Context, and usually this is enough. In a Fragment you can retrieve the context of the Activity hosting the Fragment with getActivity()

    Chante

     DBHelper DB=new DBHelper(ctxx);
    

    with

    DBHelper DB=new DBHelper(MainActivity.this);
    

    As @DerGolem pointed out, you are using the type INTEGER for the column MOTHER_NAME. Probably you want to use TEXT, instead, and you will also need a the primary key "_id"

      db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                    + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + NAME + " TEXT,"
                    + FATHER_NAME + " TEXT,"
                    + MOTHER_NAME + " TEXT"
                    + ");");
    
    0 讨论(0)
提交回复
热议问题