我已将名称传递给我的数据库,我想通过该名称提取该名称的详细信息。我想这样做。我该如何解决? “name”是单击的名称。我想使用此参数来查询数据库并获取此名称的相关信息。
public String getName(String name) {
// TODO Auto-generated method stub
String namereturned = "";
String[] columns = new String[] { KEY_ROWID, KEY_NAME,
KEY_PERSONALPHONE, KEY_HOMEPHONE, KEY_OFFICEPHONE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "="
+ name +"'", null, null, null, null);
if (c != null) {
c.moveToFirst();
return namereturned = c.getString(1);
}
return null;
}
LOG CAT
10-04 15:15:05.954: E/AndroidRuntime(760): FATAL EXCEPTION: main
10-04 15:15:05.954: E/AndroidRuntime(760): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactlist/com.example.contactlist.ViewContact}: android.database.sqlite.SQLiteException: unrecognized token: "'" (code 1): , while compiling: SELECT _id, Name, Personalnumber, Homenumber, Officenumber FROM ContactList WHERE Name=nishanth'
10-04 15:15:05.954: E/AndroidRuntime(760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.os.Handler.dispatchMessage(Handler.java:99)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.os.Looper.loop(Looper.java:137)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-04 15:15:05.954: E/AndroidRuntime(760): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 15:15:05.954: E/AndroidRuntime(760): at java.lang.reflect.Method.invoke(Method.java:511)
10-04 15:15:05.954: E/AndroidRuntime(760): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-04 15:15:05.954: E/AndroidRuntime(760): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-04 15:15:05.954: E/AndroidRuntime(760): at dalvik.system.NativeStart.main(Native Method)
10-04 15:15:05.954: E/AndroidRuntime(760): Caused by: android.database.sqlite.SQLiteException: unrecognized token: "'" (code 1): , while compiling: SELECT _id, Name, Personalnumber, Homenumber, Officenumber FROM ContactList WHERE Name=nishanth'
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
10-04 15:15:05.954: E/AndroidRuntime(760): at com.example.contactlist.DBContact.getName(DBContact.java:138)
10-04 15:15:05.954: E/AndroidRuntime(760): at com.example.contactlist.ViewContact.onCreate(ViewContact.java:31)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.app.Activity.performCreate(Activity.java:5008)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-04 15:15:05.954: E/AndroidRuntime(760): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-04 15:15:05.954: E/AndroidRuntime(760): ... 11 more
答案 0 :(得分:5)
尝试将查询更改为
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "='"
+ name +"'", null, null, null, null);
答案 1 :(得分:1)
见行:
Caused by: android.database.sqlite.SQLiteException: no such column: nishanth (code 1): , while compiling: SELECT _id, Name, Personalnumber, Homenumber, Officenumber FROM ContactList WHERE Name=nishanth
这意味着表中不存在这些列中的一个或多个。您可能在数据库表或查询中拼错了它们。看看这些。
答案 2 :(得分:0)
根据您的堆栈跟踪,您正在运行的查询是;
SELECT _id, Name, Personalnumber, Homenumber, Officenumber
FROM ContactList
WHERE Name = nishanth
正如Rajeev所说,例如,在where语句周围需要添加单引号(甚至是双引号)。
SELECT _id, Name, Personalnumber, Homenumber, Officenumber
FROM ContactList
WHERE Name = 'nishanth'
或
SELECT _id, Name, Personalnumber, Homenumber, Officenumber
FROM ContactList
WHERE Name = "nishanth"
我直接在我的SQLite数据库上运行了类似的查询(请参阅http://www.vogella.com/articles/AndroidSQLite/article.html部分&#34; 9.直接访问SQLite数据库&#34;并且不要忘记捐赠;-))并返回单引号和双引号的结果。
如果删除了引号,则会生成相同的错误消息ie。
错误:没有这样的栏目:ABC123
其中ABC123用作约束。
IMHO。如果您针对数据库,Eclipse之外或您正在使用的任何IDE或编程语言测试SQL,至少您知道您的SQL语法是正确的。通过调用代码回过头来确定问题。
当您使用&#34; =&#34;这是非常具体的,如果文本案例改变,则不匹配,例如。 &#34; nishanth&#34;和=&#34; Nishanth&#34;完全不同。
这是一个陷阱.......
在将数据插入数据库之前,我更喜欢将用户输入更改为大写(&#34; NISHANTH&#34;)或小写(&#34; nishanth&#34;)。这样,如果您从数据库中检索值并随后将其用作约束,则您知道它将匹配。
如果你想真正想要创建一个类来解析字符串值,例如,在插入数据库之前将句子大小写成大写,并在屏幕上显示值时执行反向。
相信这种解释是有用的。祝你好运。