How do I use cursors to retrieve data from a database? (Android,SQLite)
I'm stuck. I can't find out what I did wrong, so please help anyone. Although this is a very simple task, I don't understand the actual situation of the database
This is the code I use to insert data into the table in the database:
EditText title = (EditText)findViewById(R.id.Title);
String Title = title.getText().toString();
EditText d = (EditText)findViewById(R.id.director);
String Director = d.getText().toString();
sqliteDatabase db = openOrCreateDatabase("Films", MODE_PRIVATE, null);
db.execsql("CREATE TABLE IF NOT EXISTS MyFilms (idFilm INTEGER PRIMARY KEY AUTOINCREMENT, Title VARCHAR, Director VARCHAR, Year INTEGER, rating INTEGER);");
db.execsql("INSERT INTO MyFilms (Title) VALUES('" + Title + "');");
db.execsql("INSERT INTO MyFilms (Director) VALUES('" + Director + "');");
db.close();
Does this code provide me with a list containing idfilm, title, director, year and rating? If so, there is still a problem when I try to retrieve all titles from the title column using the following code:
sqliteDatabase db = openOrCreateDatabase("Films", MODE_PRIVATE, null);
Cursor c = db.rawQuery("SELECT * FROM MyFilms",null);
c.moveToFirst();
while (c.isAfterLast() == false) {
String Title = c.getString(c.getColumnIndex("Director"));
stringList.add(Title); //This I use to create listlayout dynamically and show all the Titles in it
c.moveToNext();
}
Error log:
05-21 17:50:14.666: E/AndroidRuntime(27045): FATAL EXCEPTION: main
05-21 17:50:14.666: E/AndroidRuntime(27045): java.lang.NullPointerException
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.ListView.onMeasure(ListView.java:1127)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.view.View.measure(View.java:8313)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.view.View.measure(View.java:8313)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.view.View.measure(View.java:8313)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.view.View.measure(View.java:8313)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.view.View.measure(View.java:8313)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.os.Looper.loop(Looper.java:123)
05-21 17:50:14.666: E/AndroidRuntime(27045): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-21 17:50:14.666: E/AndroidRuntime(27045): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 17:50:14.666: E/AndroidRuntime(27045): at java.lang.reflect.Method.invoke(Method.java:507)
05-21 17:50:14.666: E/AndroidRuntime(27045): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-21 17:50:14.666: E/AndroidRuntime(27045): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-21 17:50:14.666: E/AndroidRuntime(27045): at dalvik.system.NativeStart.main(Native Method)
Why does DDMS throw NullPointerException? How do I get data from the title? I tried to use c.getstring(), but when I asked for c.getstring (1), there was still a NullPointerException, but when I set c.getstring (2), it returned "director". Please help!
resolvent:
There is a problem populating the database (first snippet)
You need to combine insert statements:
db.execsql("INSERT INTO MyFilms (Title, Director, Year) VALUES('"
+ title + "','" + director + "'," + year + ");");
Otherwise, you end up with a row containing a null title or director
(Note: don't use the above SQL notation because it may make you vulnerable to SQL injection. I just write it for simplicity. In production code, you should use parameters or clean up your values. Don't be a victim of little Bobby tables.)
edit
Here are some topics about preparing statements:
> How do I use prepared statements in SQlite in Android? > Queries with prepared statements in Android?