vendredi 27 février 2015

Android : Crash using sqlite database with cursor

I'm new to android developpment and I'm trying to create a contact manager list using sqlite but i'm having troubles with accessing to my sqlite database with a cursor. I have 2 activities, one for add a new contact to the database and one for reading it for the database.


Here's my main activity :



package com.example.contactmanager;

import java.util.ArrayList;

public class MainActivity extends ActionBarActivity {

private SQLiteDatabase sdb;
private TestDBOpenHelper tdb;
private ArrayList<String> al_strings;
private ArrayAdapter<String> aa_strings;
private ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tdb = new TestDBOpenHelper(this, "test.db", null, 1);
sdb = tdb.getReadableDatabase();

lv = (ListView) findViewById(R.id.lv);
al_strings = new ArrayList<String>();
aa_strings = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, al_strings);

lv.setAdapter(aa_strings);

String[] columns = { "ID", "NAME", "PHONE_NUMBER", "EMAIL" };
Cursor c = sdb.rawQuery("test", columns);

c.moveToFirst();
while (c.isAfterLast() == false) {
al_strings.add(c.getString(c.getColumnIndex("NAME")));
al_strings.add(c.getString(c.getColumnIndex("PHONE_NUMBER")));
al_strings.add(c.getString(c.getColumnIndex("EMAIL")));
c.moveToNext();
}

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent nextScreen = new Intent(getApplicationContext(),
AddContact.class);
startActivity(nextScreen);
}
});
}

public boolean listUpdate(Integer id, String name, String phone, String mail) {

SQLiteDatabase db = tdb.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("NAME", name);
cv.put("PHONE_NUMBER", phone);
cv.put("EMAIL", mail);
db.update("test", cv, "id = ? ", new String[] { Integer.toString(id) });
return true;

}

public Cursor getList() {
SQLiteDatabase sdb = tdb.getReadableDatabase();

// the columns that we wish to retrieve from the tables
String[] columns = { "ID", "NAME", "PHONE_NUMBER", "EMAIL" };

Cursor c = sdb.rawQuery("test.db", columns);
return c;
}

// overridden method that will clear out the contents of the database
/*@Override
protected void onDestroy() {
super.onDestroy();

// run a query that will delete all the rows in our database
String table = "test";
String where = null;
String where_args[] = null;
sdb.delete(table, where, where_args);
}*/

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


Here's my 2nd activity, the one used to add contacts :



public class AddContact extends Activity {

EditText inputName;
EditText inputPhone;
EditText inputMail;
private TestDBOpenHelper tdb;
private MainActivity ma;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);

tdb = new TestDBOpenHelper(this, "test.db", null, 1);

inputName = (EditText) findViewById(R.id.name);
inputPhone = (EditText) findViewById(R.id.ed_pho);
inputMail = (EditText) findViewById(R.id.ed_ema);

Button btnPrevScreen = (Button) findViewById(R.id.btn_add);

btnPrevScreen.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

Bundle extras = getIntent().getExtras();
if(extras != null) {
int Value = extras.getInt("ID");
if(Value>0) {
if(ma.listUpdate(0, inputName.getText().toString(),
inputPhone.getText().toString(),
inputMail.getText().toString())) {
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getApplicationContext(), "Not updated",
Toast.LENGTH_SHORT).show();
}
else {
if(addContact(inputName.getText().toString(),
inputPhone.getText().toString(),
inputMail.getText().toString())) {
Toast.makeText(getApplicationContext(), "Contact Added",
Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getApplicationContext(), "Error",
Toast.LENGTH_SHORT).show();
}
Intent prevScreen = new Intent(getApplicationContext(), MainActivity.class);
startActivity(prevScreen);
}
}
});
}

public boolean addContact(String name, String phone, String mail) {

SQLiteDatabase sdb = tdb.getWritableDatabase();
ContentValues cv = new ContentValues();

cv.put("NAME", name);
cv.put("PHONE_NUMBER", phone);
cv.put("EMAIL", mail);

sdb.insert("test", null, cv);

return true;
}
}


The problem is here, on my MainActivity :



Cursor c = sdb.rawQuery("test", columns);


When I put this into comments, my app "works" (I can launch it from my nexus 5) and I found out through the debug mode that it's specificly THIS line that makes it crash (I think).


Here's the LogCat :



02-27 17:59:04.408: E/SQLiteLog(28845): (1) near "test": syntax error
02-27 17:59:04.414: D/AndroidRuntime(28845): Shutting down VM
02-27 17:59:04.418: E/AndroidRuntime(28845): FATAL EXCEPTION: main
02-27 17:59:04.418: E/AndroidRuntime(28845): Process: com.example.contactmanager, PID: 28845
02-27 17:59:04.418: E/AndroidRuntime(28845): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactmanager/com.example.contactmanager.MainActivity}: android.database.sqlite.SQLiteException: near "test": syntax error (code 1): , while compiling: test
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.os.Handler.dispatchMessage(Handler.java:102)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.os.Looper.loop(Looper.java:135)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-27 17:59:04.418: E/AndroidRuntime(28845): at java.lang.reflect.Method.invoke(Native Method)
02-27 17:59:04.418: E/AndroidRuntime(28845): at java.lang.reflect.Method.invoke(Method.java:372)
02-27 17:59:04.418: E/AndroidRuntime(28845): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-27 17:59:04.418: E/AndroidRuntime(28845): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-27 17:59:04.418: E/AndroidRuntime(28845): Caused by: android.database.sqlite.SQLiteException: near "test": syntax error (code 1): , while compiling: test
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
02-27 17:59:04.418: E/AndroidRuntime(28845): at com.example.contactmanager.MainActivity.onCreate(MainActivity.java:44)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.app.Activity.performCreate(Activity.java:5933)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-27 17:59:04.418: E/AndroidRuntime(28845): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-27 17:59:04.418: E/AndroidRuntime(28845): ... 10 more


I'm trying to solve this for hours but I can't find a way, any help is blessed. It might a stupid error from me, forgive me in advance.


Thanks,


Blareach


Aucun commentaire:

Enregistrer un commentaire