This is my update query,
ContentValues value = new ContentValues();
value.put(DBhelper.Amount, txtBudget.getText().toString());
db = helper.getWritableDatabase();
db.update(DBhelper.TABLE1, value," "+DBhelper.Description+"='"+value_in_tv,null);
db.close();
fetchData2();
Toast.makeText(this, "Update Successfully", Toast.LENGTH_LONG).show();
clearfield();
when click the button to update,it's giving fatal exception error.
value_in_tv this is the value I got from another class
Bundle data_from_list= getIntent().getExtras();
value_in_tv= data_from_list.getString("passed data key");
txr.setText(value_in_tv);
Error:
10-18 20:45:57.812 18632-18632/com.example.username.weddingplanning
E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: unrecognized token: "'suba" (code 1): , while compiling: UPDATE Category SET amount=?
WHERE description='suba
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:686)
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.updateWithOnConflict(SQLiteDatabase.java:1669)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1620)
at com.example.username.weddingplanning.addbudget.checkIfRowPresent(addbudget.java:134)
at com.example.username.weddingplanning.addbudget.onClick(addbudget.java:114)
at android.view.View.performClick(View.java:4439)
at android.view.View$PerformClick.run(View.java:18398)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
You forgot the closing ' in the where clause:
db.update(DBhelper.TABLE1, value," "+DBhelper.Description+"='"+value_in_tv + "'",null)
Related
I want to connect my login page to MySQL PHP but I got some error here.
This is my logcat:
807/com.aeu.mlibrary.mlibraryaeu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aeu.mlibrary.mlibraryaeu, PID: 1807
java.lang.ClassCastException: com.aeu.mlibrary.mlibraryaeu.LoginFragment cannot be cast to android.content.Context
at com.kosalgeek.asynctask.PostResponseAsyncTask.<init>(PostResponseAsyncTask.java:284)
at com.aeu.mlibrary.mlibraryaeu.LoginFragment.onClick(LoginFragment.java:82)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
and this is the error line in my loginFragment.java:
#Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString());
PostResponseAsyncTask task = new PostResponseAsyncTask(LoginFragment.this, postData);
task.execute("http://10.0.3.2/mlibrary/HTML/login.php");
}
I need your help guys!
Thank you.
Replace LoginFragment.this with getActvity()
PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), postData);
Replace LoginFragment.this with getContext() :
PostResponseAsyncTask task = new PostResponseAsyncTask(getContext(), postData);
I have called a function returnCurrentNameById() by passing the id of the particular row. But it gives an error.
String[] s1;
public String[] returnCurrentNameById(int k) throws SQLException{
String[] columns = new String[]{ KEY_ID1, KEY_NAME, Key_DOB, KEY_AGE, KEY_PHONE_NO, Key_EXPERIENCE, KEY_EMAIL, KEY_STATUS, KEY_STATUS};
SQLiteDatabase db3 = this.getReadableDatabase();
Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + "=" + k, null, null, null, null);
if (c != null){
c.moveToFirst();
for(int i = 0; i<=8; i++){
s1[i] = c.getString(i);
}
return s1;
}
return null;
}
The Log cat error report is:
02-15 16:38:13.424 1545-1545/world.com.my`enter code here`progect09 D/AndroidRuntime﹕ Shutting down VM
02-15 16:38:13.464 1545-1545/world.com.myprogect09 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x2bc9a300)
02-15 16:38:13.533 1545-1545/world.com.myprogect09 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3591)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at world.com.myprogect09.DataBaseSQL.returnCurrentNameById1(DataBaseSQL.java:209)
at world.com.myprogect09.ThirdActivity.isClicked(ThirdActivity.java:74)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3586)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
02-15 16:38:34.491 1545-1545/world.com.myprogect09 I/Process﹕ Sending signal. PID: 1545 SIG: 9
You have not initialized your s1 array.
Use
s1 = new String[9];
to allocate an array of 9 elements.
Also check the return value of moveToFirst() so you don't get an exception in case the query returns no rows.
Replace
Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + "=" + k, null, null, null, null);
with
Cursor c = db3.query(DATA_BASE_TABLE_NAME1, columns, KEY_ID1 + " = ?", new String[]{String.valueOf(k)}, null, null, null);
I am about to freaking blow my brains out if I don't get this and I have no idea what is wrong. I am just trying to get the result from a query into a database file called Test that I made from SQLiteDatabase Browser.
I have MyDB class and my MainActivity class which just has a button and a textView (all this is for testing the query so it can be put into my app later). I'am trying to make it so that when you push that button it does the rawQuery in the MyDB class and then displays it on the textView. I will have the names for the rows match up with buttons in the app and will then be inserted in the raw query (SQL style) so that It will chose the proper result story based on the other columns.
I have tried this same hard coded query in queryMethodWithout() in my SQL Browser and it works just fine.
The logCat is giving me
IllegalStateException: could not execute method of the Activity
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.NullPointerException
With this query I have everything hard coded to specific columns and a speific table just to make sure it all works but It won't print out into the text view, can someone please help?!
I know this is selfish but I am dying right meow!
Cursor from the MyDB class:
package com.example.dbquerytester;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class MyDB extends SQLiteAssetHelper{
public static final String DATABASE_NAME = "Test.db";
public static final int DATABASE_VERSION = 1;
Context context;
public MyDB(Context applicationContext) {
super(applicationContext, DATABASE_NAME, null, DATABASE_VERSION);
this.context = applicationContext;
}
public Cursor queryMethodWithout(){
SQLiteDatabase database = this.getReadableDatabase();
String storyQueryWithout1 = "SELECT story FROM Christian WHERE Target = 'Immediate' AND Target2 = 'Mother' AND Variable = 'Courage'";
Cursor cursor = database.rawQuery(storyQueryWithout, null);
return cursor;
}
And here's my MainActivity (The onClickQuery() method is the one that is called when the button is pressed):
public class Main_Activity extends Activity {
MyDB myDB;
private Cursor cursorQuery;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void displayMessage(String s){
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(s);
}
public void openDB() throws SQLiteException{
myDB = new MyDB(this);
myDB.getReadableDatabase();
}
public void onClickQuery(View view){
openDB();
cursorQuery = myDB.queryMethodWithout();
cursorQuery.moveToFirst();
String story = cursorQuery.getString(0);
displayMessage(story);
}
protected void onDestroy(){
super.onDestroy();
cursorQuery.close();
myDB.close();
}
}
Here is my full LogCat!
07-17 11:47:34.760: D/(945): HostConnection::get() New Host Connection established 0xb823def0, tid 945
07-17 11:47:34.990: W/EGL_emulation(945): eglSurfaceAttrib not implemented
07-17 11:47:35.010: D/OpenGLRenderer(945): Enabling debug mode 0
07-17 11:47:37.770: I/SQLiteAssetHelper(945): successfully opened database Test.db
07-17 11:47:37.770: D/AndroidRuntime(945): Shutting down VM
07-17 11:47:37.780: W/dalvikvm(945): threadid=1: thread exiting with uncaught exception (group=0xb3a3dba8)
07-17 11:47:37.800: E/AndroidRuntime(945): FATAL EXCEPTION: main
07-17 11:47:37.800: E/AndroidRuntime(945): Process: com.example.dbquerytester, PID: 945
07-17 11:47:37.800: E/AndroidRuntime(945): java.lang.IllegalStateException: Could not execute method of the activity
07-17 11:47:37.800: E/AndroidRuntime(945): at android.view.View$1.onClick(View.java:3823)
07-17 11:47:37.800: E/AndroidRuntime(945): at android.view.View.performClick(View.java:4438)
07-17 11:47:37.800: E/AndroidRuntime(945): at android.view.View$PerformClick.run(View.java:18422)
07-17 11:47:37.800: E/AndroidRuntime(945): at android.os.Handler.handleCallback(Handler.java:733)
07-17 11:47:37.800: E/AndroidRuntime(945): at android.os.Handler.dispatchMessage(Handler.java:95)
07-17 11:47:37.800: E/AndroidRuntime(945): at android.os.Looper.loop(Looper.java:136)
07-17 11:47:37.800: E/AndroidRuntime(945): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-17 11:47:37.800: E/AndroidRuntime(945): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:47:37.800: E/AndroidRuntime(945): at java.lang.reflect.Method.invoke(Method.java:515)
07-17 11:47:37.800: E/AndroidRuntime(945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-17 11:47:37.800: E/AndroidRuntime(945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-17 11:47:37.800: E/AndroidRuntime(945): at dalvik.system.NativeStart.main(Native Method)
07-17 11:47:37.800: E/AndroidRuntime(945): Caused by: java.lang.reflect.InvocationTargetException
07-17 11:47:37.800: E/AndroidRuntime(945): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:47:37.800: E/AndroidRuntime(945): at java.lang.reflect.Method.invoke(Method.java:515)
07-17 11:47:37.800: E/AndroidRuntime(945): at android.view.View$1.onClick(View.java:3818)
07-17 11:47:37.800: E/AndroidRuntime(945): ... 11 more
07-17 11:47:37.800: E/AndroidRuntime(945): Caused by: java.lang.NullPointerException
07-17 11:47:37.800: E/AndroidRuntime(945): at com.example.dbquerytester.MyDB.queryMethodWithout(MyDB.java:68)
07-17 11:47:37.800: E/AndroidRuntime(945): at com.example.dbquerytester.Main_Activity.onClickQuery(Main_Activity.java:67)
07-17 11:47:37.800: E/AndroidRuntime(945): ... 14 more
I'm trying to add an SQLite database to my android app.
I made a separate class for the pin table (Pin.java) and
a database handler in MySQLiteHelper.java
Now I'm trying to call the addPin method in one of my activities and
it gives me an error:
http://pastebin.com/8h4k3Quh
11-20 18:39:44.011: E/SQLiteLog(22644): (1) no such table: tblPins
11-20 18:39:44.021: E/SQLiteDatabase(22644): Error inserting pinMessage=testinh pinDuration=1 pinTitle=test
11-20 18:39:44.021: E/SQLiteDatabase(22644): android.database.sqlite.SQLiteException: no such table: tblPins (code 1): , while compiling: INSERT INTO tblPins(pinMessage,pinDuration,pinTitle) VALUES (?,?,?)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at com.github.viperdream.MySQLiteHelper.addPin(MySQLiteHelper.java:54)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at com.github.viperdream.AddPinActivity.btnCreatePin(AddPinActivity.java:39)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at java.lang.reflect.Method.invoke(Method.java:525)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.view.View$1.onClick(View.java:3628)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.view.View.performClick(View.java:4240)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.view.View$PerformClick.run(View.java:17721)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.os.Handler.handleCallback(Handler.java:730)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.os.Looper.loop(Looper.java:137)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at java.lang.reflect.Method.invoke(Method.java:525)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 18:39:44.021: E/SQLiteDatabase(22644): at dalvik.system.NativeStart.main(Native Method)
11-20 18:39:44.021: E/SQLiteLog(22644): (1) no such table: tblPins
11-20 18:39:44.021: D/AndroidRuntime(22644): Shutting down VM
11-20 18:39:44.021: W/dalvikvm(22644): threadid=1: thread exiting with uncaught exception (group=0x415df700)
11-20 18:39:44.021: E/AndroidRuntime(22644): FATAL EXCEPTION: main
11-20 18:39:44.021: E/AndroidRuntime(22644): java.lang.IllegalStateException: Could not execute method of the activity
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.view.View$1.onClick(View.java:3633)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.view.View.performClick(View.java:4240)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.view.View$PerformClick.run(View.java:17721)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.os.Handler.handleCallback(Handler.java:730)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.os.Looper.loop(Looper.java:137)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-20 18:39:44.021: E/AndroidRuntime(22644): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:39:44.021: E/AndroidRuntime(22644): at java.lang.reflect.Method.invoke(Method.java:525)
11-20 18:39:44.021: E/AndroidRuntime(22644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-20 18:39:44.021: E/AndroidRuntime(22644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 18:39:44.021: E/AndroidRuntime(22644): at dalvik.system.NativeStart.main(Native Method)
11-20 18:39:44.021: E/AndroidRuntime(22644): Caused by: java.lang.reflect.InvocationTargetException
11-20 18:39:44.021: E/AndroidRuntime(22644): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 18:39:44.021: E/AndroidRuntime(22644): at java.lang.reflect.Method.invoke(Method.java:525)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.view.View$1.onClick(View.java:3628)
11-20 18:39:44.021: E/AndroidRuntime(22644): ... 11 more
11-20 18:39:44.021: E/AndroidRuntime(22644): Caused by: android.database.sqlite.SQLiteException: no such table: tblPins (code 1): , while compiling: Select * FROM tblPins WHERE pinTitle = "test"
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
11-20 18:39:44.021: E/AndroidRuntime(22644): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
11-20 18:39:44.021: E/AndroidRuntime(22644): at com.github.viperdream.MySQLiteHelper.findPin(MySQLiteHelper.java:61)
11-20 18:39:44.021: E/AndroidRuntime(22644): at com.github.viperdream.AddPinActivity.btnCreatePin(AddPinActivity.java:41)
It says it can't find the table tblPins, but I thought it'd create automatically after calling the function.
I can't seem to figure this one out, here's my code:
Databasehandler, MySQLiteHelper.java:
public class MySQLiteHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "PinDB";
private static final String TABLE_PINS = "tblPins";
private static final String KEY_PINID = "pin_id";
private static final String KEY_PINTITLE = "pinTitle";
private static final String KEY_PINMESSAGE = "pinMessage";
private static final String KEY_PINDURATION = "pinDuration";
private static final String[] COLUMNS = {KEY_PINID, KEY_PINTITLE, KEY_PINMESSAGE, KEY_PINDURATION};
public MySQLiteHelper(Context context, String name, CursorFactory factory, int version){
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE "
+ TABLE_PINS + " ("
+ KEY_PINID +" INTEGER PRIMARY KEY," + KEY_PINTITLE +
" TEXT,"+
KEY_PINMESSAGE+" TEXT,"
+ KEY_PINDURATION +" INTEGER "+ ")");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersio, int newVersion){
db.execSQL("DROP TABLE IF EXISTS tblPins");
this.onCreate(db);
}
public void addPin(Pin pin){
Log.d("addPin", pin.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PINTITLE, pin.getPinTitle());
values.put(KEY_PINMESSAGE, pin.getPinMessage());
values.put(KEY_PINDURATION, pin.getPinDuration());
db.insert(TABLE_PINS, null, values);
db.close();
}
public Pin findPin(String pinTitle){
String query = "Select * FROM " + TABLE_PINS + " WHERE " + KEY_PINTITLE + " = \"" + pinTitle +"\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Pin pin = new Pin();
if (cursor.moveToFirst()){
cursor.moveToFirst();
pin.setPinID(Integer.parseInt(cursor.getString(0)));
pin.setPinTitle(cursor.getString(1));
pin.setPinMessage(cursor.getString(2));
pin.setPinDuration(Integer.parseInt(cursor.getString(3)));
cursor.close();
}else{
pin = null;
}
db.close();
return pin;
}
public boolean deletePin(String pinTitle){
boolean result = false;
String query = "Select * FROM " + TABLE_PINS + " WHERE " + KEY_PINTITLE + " = \"" + pinTitle + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Pin pin = new Pin();
if(cursor.moveToFirst()){
pin.setPinID(Integer.parseInt(cursor.getString(0)));
db.delete(TABLE_PINS, KEY_PINID + " = ?",
new String[] { String.valueOf(pin.getID()) });
cursor.close();
result = true;
}
db.close();
return result;
}
}
If you need any more information, please do not hesitate to ask!
Thanks in advance
Update: I've managed to get the table in, I've put onCreate() in the addPin method, but is there any reason why this method doesn't get called when I don't put it in addPin?
I'm trying to retrieve the data from my database through a cursor object once the database has been queried. I seem to be getting an error on my cursor.close() and when closing my database. This has been fine up until the point but I started receiving 'cursor finalizer' errors when accessing classes from different intents.
At the moment I seem to have my cursor objects closing in an incorrect manner.
As an example heres how Im trying to retrieve all the data to populate a listview within the onCreate method of my class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appointmentview);
searchedAppView = (ListView)findViewById(android.R.id.list);
DBHandlerApp DBAppointments = new DBHandlerApp(this, null, null);
DBHandlerApp searchApps = new DBHandlerApp(this, null, null);
searchApps.open();
Cursor cursor = searchApps.getAppointmentsData();
startManagingCursor(cursor);
#SuppressWarnings("static-access")
String [] from = new String [] {DBAppointments.KEY_NAMEAPP, DBAppointments.KEY_TYPEAPP, DBAppointments.KEY_TIMEAPP, DBAppointments.KEY_DATEAPP, DBAppointments.KEY_COMMENTAPP};
int [] to = new int [] {R.id.txtAppointName, R.id.txtAppointType, R.id.txtAppointTime, R.id.txtAppointDate, R.id.txtAppointCom};
#SuppressWarnings("deprecation")
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.setappointviews, cursor, from, to);
searchedAppView.setAdapter(cursorAdapter);
searchAppoints = (ImageButton) findViewById(R.id.btnSearchAppointName);
searchAppName = (EditText) findViewById(R.id.inputAppointName);
searchAppoints.setOnClickListener(this);
}
Heres the method 'getAppointmentData' of the DBHandlerApp class:
public Cursor getAppointmentsData() {
String [] columns = new String[]{KEY_ROWAPPID, KEY_NAMEAPP, KEY_TYPEAPP, KEY_TIMEAPP, KEY_DATEAPP, KEY_COMMENTAPP};
Cursor c = ourDatabase.query(DATABASE_TABLEAPP, columns, null, null, null, null, KEY_NAMEAPP + " ASC", null);
//*****Closing the cursor object.*****
c.close();
return c;
Logcat error:
01-31 17:44:34.220: E/AndroidRuntime(269): FATAL EXCEPTION: main
01-31 17:44:34.220: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.ViewAppointments}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.os.Looper.loop(Looper.java:123)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 17:44:34.220: E/AndroidRuntime(269): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 17:44:34.220: E/AndroidRuntime(269): at java.lang.reflect.Method.invoke(Method.java:521)
01-31 17:44:34.220: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 17:44:34.220: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 17:44:34.220: E/AndroidRuntime(269): at dalvik.system.NativeStart.main(Native Method)
01-31 17:44:34.220: E/AndroidRuntime(269): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.support.v4.widget.CursorAdapter.getCount(CursorAdapter.java:202)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.widget.ListView.setAdapter(ListView.java:436)
01-31 17:44:34.220: E/AndroidRuntime(269): at com.example.flybase2.ViewAppointments.onCreate(ViewAppointments.java:54)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 17:44:34.220: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-31 17:44:34.220: E/AndroidRuntime(269): ... 11 more
Currently at the moment I do not close the database using .searchApps.close();. If do this then I get the following logcat error:
01-31 17:52:17.324: E/AndroidRuntime(277): FATAL EXCEPTION: main
01-31 17:52:17.324: E/AndroidRuntime(277): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.ViewAppointments}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.os.Looper.loop(Looper.java:123)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-31 17:52:17.324: E/AndroidRuntime(277): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 17:52:17.324: E/AndroidRuntime(277): at java.lang.reflect.Method.invoke(Method.java:521)
01-31 17:52:17.324: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-31 17:52:17.324: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-31 17:52:17.324: E/AndroidRuntime(277): at dalvik.system.NativeStart.main(Native Method)
01-31 17:52:17.324: E/AndroidRuntime(277): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT _id, app_name, app_type, app_time, app_date, app_comments FROM appointmentsTable ORDER BY app_name ASC)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.support.v4.widget.CursorAdapter.getCount(CursorAdapter.java:202)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.widget.ListView.setAdapter(ListView.java:436)
01-31 17:52:17.324: E/AndroidRuntime(277): at com.example.flybase2.ViewAppointments.onCreate(ViewAppointments.java:56)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 17:52:17.324: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-31 17:52:17.324: E/AndroidRuntime(277): ... 11 more
You're closing your cursor in the wrong place. You should close a cursor when you're completely done using it, you're closing it right after populating it, before returning it from your getAppointmentData method.
Simply removing the close call from that method should fix your problem because when you use startManagingCursor, the activity handles closing it.
As an aside, startManagingCursor is depreciated in favor of using Loaders.
I do not exactly know what the problem is. However, the method startManagingCursor(...) is deprecated and should not be used, see: http://developer.android.com/reference/android/app/Activity.html#startManagingCursor(android.database.Cursor)
A better way is to use a CursorLoader.