Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 运行SQLiteQuery时出错_Android_Sql_Sqlite - Fatal编程技术网

Android 运行SQLiteQuery时出错

Android 运行SQLiteQuery时出错,android,sql,sqlite,Android,Sql,Sqlite,我试图保存一个分数,如果它不存在。如果它可用,那么我需要从数据库中获得最高分数 SQLiteDatabase db= openOrCreateDatabase("tryitdb", MODE_PRIVATE, null); String sql= "create table if not exists tabletry(score int)"; db.execSQL(sql); db.close();

我试图保存一个分数,如果它不存在。如果它可用,那么我需要从数据库中获得最高分数

SQLiteDatabase db= openOrCreateDatabase("tryitdb", MODE_PRIVATE, null);
            String sql= "create table if not exists tabletry(score int)";
            db.execSQL(sql);
            db.close();
            tv1= (TextView) findViewById(R.id.textView1);
            et1= (EditText) findViewById(R.id.editText1);
            b1 = (Button) findViewById(R.id.button1);
            b1.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    getsc();
                }
            });
        };
        public void getsc() {
            SQLiteDatabase db= openOrCreateDatabase("tryitdb", MODE_PRIVATE, null);
            String sql= "select * from tabletry";
            Cursor c1= db.rawQuery(sql, null);
            if(c1.isNull(0)){
                init();
            }
            else{
                int i1= c1.getColumnIndex("score");
                String l= c1.getString(i1);
                int old= Integer.valueOf(l);
                int str= Integer.valueOf(et1.getText().toString());
                if(old>=str){
                    tv1.setText(old);
                }
                else{
                    tv1.setText(str);
                }
            }

        }
        public void init() {
             SQLiteDatabase db= openOrCreateDatabase("tryitdb", MODE_PRIVATE, null);
             String string= "insert into tabletry values(?,?)";
             Object[] o= new Object[1];
             o[0] = et1.getText().toString();
             db.execSQL(string,o);
             db.close();
        }
stacktrace告诉我没有像table try这样的表。 也给了我一个关于特定行的错误

Cursor c1= db.rawQuery(sql, null);

请建议解决此问题的方法。谢谢。

更改>
db.equals(sql)
to
db.execSQL(sql)

请管理您的光标

  if (cursor != null && cursor.getCount() > 0) {

           c.moveToFirst(); 
                    // your logic goes here
                } else {
                    Toast.makeText(getApplicationContext(), "No Record Found", 1000).show();
                }
如果您有多个数据,那么您可以尝试

 if (cursor != null && cursor.getCount() > 0) {

   if (cur.moveToFirst()) {
  do {


    cur.getString(0); //any thing what you want to fetch

                    } while (cur.moveToNext());
                }}

获取CursorIndexOutfound异常在将其更改为execSQL之后,它仍然会在if(c1.isNull(0))上给我一个异常