rawQuery不是在SQLite的Andr​​oid平台 [英] rawQuery not working in SQLite android

查看:118
本文介绍了rawQuery不是在SQLite的Andr​​oid平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有我想要做的就是设置一个字符串,等于我的数据库的一个条目。我正在访问该表有1列(除了ID列)。我一直在挣扎与此一段时间,因此,即使类似的问题一直在问,我不能让这种情况下工作。我很舒服,在SQLite的其他一些命令,但仍然是一个初学者。这里是什么我有。我是pretty确保一旦这种方法适用花药类我的电话将正常工作。

All i want to do is set a string equal to one of the entries in my database. The table i am accessing has 1 column (besides the ID column). I have been struggling with this for awhile so and even though similar questions have been asked, i am unable to make this situation work. I am comfortable with some other commands in SQLite but still a beginner. Here is what i have. I'm pretty sure once this method works my call from anther class will work fine.

请注意,这两个StringCategory_Table和COLUMN_CATEGORIES被定义为字符串。

note that both StringCategory_Table and COLUMN_CATEGORIES are defined as Strings.

我的数据库具有以下(正常工作以外以下方法一切)创建:

my database was created with the following (it works fine for everything else except the below method):

db.execSQL("CREATE TABLE " + StringCategory_Table + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_CATEGORIES + " TEXT NOT NULL);");

和这里是有问题(使用SQLite开放助手在数据库类创建)的方法:

and here is the method in question (created in a database class using SQLite open helper):

public String getCategory(int catId){
Cursor mycursor = myDatabase.rawQuery("SELECT " + COLUMN_CATEGORIES  + " FROM "+ StringCategory_Table +" WHERE " + _ID + " = " + catId, null);

//i no longer use this line:String strCatName = mycursor.toString();


String strCatName = mycursor.getString(mycursor.getColumnIndex(COLUMN_CATEGORIES));

        return strCatName;
    }

这是我对上述code logcat的:

09-04 20:05:47.764: E/AndroidRuntime(9232): FATAL EXCEPTION: main
09-04 20:05:47.764: E/AndroidRuntime(9232): java.lang.RuntimeException: Unable to instantiate activity  ComponentInfo{com.mypackage.namespace/com.mypackage.namespace.MyCurrentRestaurants}: java.lang.NullPointerException
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.os.Looper.loop(Looper.java:137)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at java.lang.reflect.Method.invoke(Method.java:511)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-04 20:05:47.764: E/AndroidRuntime(9232):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

而不是删除这个问题,我将解释什么的问题是(因为这将是恼人通过评论挖):


  1. 我不能只分配一个类字符串到一个SQLite条目我在做什么。相反,我不得不打开数据库之前,我可以从中读取:因为我需要打开数据库之前,我可以从中读取,接下去我需要创建SQLite数据库类的一个实例

  2. 由于我使用的是光标,我需要使用moveToFirst方法之前任何事情都会正常工作。

感谢大家今天谁帮助了我,请让我知道,如果这些结论是不正确的,因为我不想给S $ P $垫的误解,以人才为困惑的我。
亚当

Thanks to everyone who has helped me today, and please let me know if these conclusions are incorrect as i don't want to spread misconceptions to people as confused as i am. Adam

推荐答案

试试这个,

public String getCategory(int catId){
        Cursor mycursor = myDatabase.rawQuery("SELECT " + COLUMN_CATEGORIES  + " FROM "+ StringCategory_Table +" WHERE " + _ID + " = " + catId, null);
      if(mycursor.moveToFirst()){ //Edited based on suggestion from SAM
          String strCatName = mycursor.getString(mycursor.getColumnIndex(COLUMN_CATEGORIES));
                  return strCatName;
        } else {
        return null;
         }


        }

这篇关于rawQuery不是在SQLite的Andr​​oid平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆