Reputation: 5905
I am trying to save data in SQLite but getting exception, I checked everything but still getting error.
Here is my logcat:
03-01 07:50:51.240: E/AndroidRuntime(3984): FATAL EXCEPTION: main
03-01 07:50:51.240: E/AndroidRuntime(3984): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appstage.babyimmunisation/com.appstage.babyimmunisation.Notes}: android.database.sqlite.SQLiteException: near "create": syntax error: , while compiling: DROP TABLE IF EXISTS create table IF NOT EXISTS babydetails( notes_id integer primary key autoincrement, babyname text not null, babyIMAGE text not null, babygender text not null, babydob text not null, babybloodgroup text not null);
03-01 07:50:51.240: E/AndroidRuntime(3984): Caused by: android.database.sqlite.SQLiteException: near "create": syntax error: , while compiling: DROP TABLE IF EXISTS create table IF NOT EXISTS babydetails( notes_id integer primary key autoincrement, babyname text not null, babyIMAGE text not null, babygender text not null, babydob text not null, babybloodgroup text not null);
03-01 07:50:51.240: E/AndroidRuntime(3984): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-01 07:50:51.240: E/AndroidRuntime(3984): at com.appstage.babyimmunisation.Notes.onCreate(Notes.java:31)
Here is my code:
AddNotes.java
DBHelper db;
GetSet gs = new GetSet();
btnAddNotes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
db=new DBHelper(getBaseContext());
db.getWritableDatabase();
gs.setNotes(notes);
db.addNotes(gs);
//db.close();
}
});
DBHelper.java
private static final String DATABASE_NOTES_CREATE = "create table IF NOT EXISTS "
+ TABLE_NOTES + "( " + COLUMN_NOTES_ID
+ " integer primary key autoincrement, "
+ COLUMN_NOTES + " text not null);";
private static final String DATABASE_BABY_CREATE = "create table IF NOT EXISTS "
+ TABLE_BABYDETAILS + "( " + COLUMN_BABYID
+ " integer primary key autoincrement, "
+ COLUMN_BABYNAME + " text not null, "
+ COLUMN_BABYIMAGE + " text not null, "
+ COLUMN_BABYGENDER + " text not null, "
+ COLUMN_BABYDOB + " text not null, "
+ COLUMN_BABYBLOODGROUP + " text not null);";
public DBHelper open() throws SQLException
{
db = this.getWritableDatabase();
return this;
}
public DBHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database)
{
database.execSQL(DATABASE_BABY_CREATE);
database.execSQL(DATABASE_NOTES_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion)
{
Log.w(DBHelper.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
//database.execSQL("DROP TABLE IF EXISTS " + DATABASE_NOTES_CREATE);
database.execSQL("DROP TABLE IF EXISTS " + DATABASE_BABY_CREATE);
database.execSQL("DROP TABLE IF EXISTS " + DATABASE_NOTES_CREATE);
onCreate(database);
}
//getting all notes details
public List<GetSet>getAllNotes()
{
List<GetSet>LocwiseProfileList=new ArrayList<GetSet>();
String selectQuery= "SELECT * FROM " + TABLE_NOTES;
db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst())
{
do {
GetSet owq= new GetSet();
owq.setNotesId(cursor.getInt(0));
owq.setNotes(cursor.getString(1));
LocwiseProfileList.add(owq);
}
while(cursor.moveToNext());
db.close();
}
return LocwiseProfileList;
}
public void addNotes(GetSet gs)
{
try
{
SQLiteDatabase db= this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NOTES, gs.getNotes());
db.insert(TABLE_NOTES, null, values);
db.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void addBabyDetails(GetSet gs) {
// TODO Auto-generated method stub
try
{
SQLiteDatabase db= this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_BABYNAME, gs.getAddBabyName());
values.put(COLUMN_BABYDOB, gs.getAddBabyDOB());
values.put(COLUMN_BABYGENDER, gs.getAddBabyGender());
values.put(COLUMN_BABYBLOODGROUP, gs.getAddBabyBloodGroup());
values.put(COLUMN_BABYIMAGE, gs.getImagePath());
db.insert(TABLE_BABYDETAILS, null, values);
db.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Please give me any reference or hint about my mistake or where i am wrong?
Upvotes: 0
Views: 536
Reputation: 20043
I think database.execSQL("DROP TABLE IF EXISTS " + DATABASE_BABY_CREATE);
should rather be
database.execSQL("DROP TABLE IF EXISTS " + TABLE_BABYDETAILS);
And the same for the other table. However, you should try to reduce your question to the essential parts rather than posting as much source code as you can. Plus, considering that an exception is thrown in Notes.java
, adding (the relevant part of) that file would be helpful.
Upvotes: 1