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
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.....).
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.
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);
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"
+ ");");