gpt4 book ai didi

android - 如何使用游标从数据库中检索数据? (安卓,SQLite)

转载 作者:太空宇宙 更新时间:2023-11-03 11:38:07 24 4
gpt4 key购买 nike

我卡住了。我找不到我做错了什么,所以请帮助任何人。尽管这是一项非常简单的任务,但我不明白数据库的实际情况。

这是我用来将数据插入数据库表的代码:

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();

这段代码是否为我提供了一个表格,其中包含 idfilmTitleDirectorYear评级?如果是这样,当我尝试使用以下代码从 Title 列中检索所有标题时仍然存在问题:

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();
}

错误日志:

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)

为什么DDMS会抛出NullPointerException?我应该如何从“标题”中获取数据?我尝试使用 c.getString(),但是当我请求 c.getString(1) 时,仍然存在 NullPointerException,尽管当我设置 c.getString(2) 时它返回“Director”。请帮忙!

最佳答案

如果您从编辑文本框中正确插入值。然后执行这些步骤。 检索值

Cursor c = db.rawQuery("SELECT * FROM MyFilms",null);

if (c != null ) {
if (c.moveToFirst()) {
do {
String dir = c.getString(c.getColumnIndex("Director"));
test.add("" + dir );
}while (c.moveToNext());
}
}

将值显示为列表

    this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,test));

当类扩展 ListActivity 时,该语句将其显示为列表。

关于android - 如何使用游标从数据库中检索数据? (安卓,SQLite),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10690018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com