我正在实现一个SQLite数据库。在数据库中,我正在存储类型为Contact
的对象,其定义如下。方法getContact()
应该采用id
并返回数据库中相应的Contact
对象。但这不起作用。当我调试它时,控件就会卡在线上,然后应用程序崩溃了。
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME_DEFAULT,KEY_NAME,KEY_IMAGE}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
我对SQLite有一个非常基本的了解,无法弄清楚这里有什么问题。请帮忙。
联系班级
package com.example.nirvan.diary30;
import android.graphics.Bitmap;
public class Contact
{
String nameDefault;
public String name[];
public Bitmap image[];
public int id;
Contact(int id, String name,String arrName[],Bitmap arrImage[])
{
this.id=id;
this.nameDefault=name;
this.name=arrName;
this.image=arrImage;
}
}
在DatabaseHandler.java中getContact()
Contact getContact(int id)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME_DEFAULT,KEY_NAME,KEY_IMAGE}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1) ,convertStringToArray(cursor.getString(2)) , backConverter(cursor.getString(3)));
return contact;
}
更新 的登录
FATAL EXCEPTION: main
Process: com.example.nirvan.diary30, PID: 6937
android.database.sqlite.SQLiteException: no such column: image (code 1): , while compiling: SELECT id, nameDefault, name, image FROM contacts WHERE id=?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1238)
at com.example.nirvan.diary30.DatabaseHandler.getContact(DatabaseHandler.java:136)
at com.example.nirvan.diary30.MainActivity$1.onPageSelected(MainActivity.java:87)
at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1933)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:685)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:669)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)
at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2156)
at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1138)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1132)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1105)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1396)
at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1501)
at android.view.View$PerformClick.run(View.java:18465)
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:5086)
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)
答案 0 :(得分:1)
您的query()
声明看起来不错。您需要检查cursor.moveToFirst()
是否已返回true
。如果false
为空,则返回cursor
。有关详细信息,请参阅here
试试这个:
if (cursor.moveToFirst())
{
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
convertStringToArray(cursor.getString(2)),
backConverter(cursor.getString(3)));
}
else
{
// not found
}