【问题标题】:error inserting and executing query in android sqlite database在 android sqlite 数据库中插入和执行查询时出错
【发布时间】:2016-12-23 23:51:57
【问题描述】:

所以在我的应用程序中,我使用了 get 和 set 方法。当它执行 set 方法时,应用程序不会崩溃,但是当它执行 get 方法时,它会崩溃。我发现当它到达 get 方法中的 db.query() 行时它会崩溃,但我认为问题也可能出在 set 方法中,因为它们都会给出错误消息。

这是我的代码:

公共类 MyDBHandler 扩展 SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String COLUMN_ID = "_id";
public static final String TABLE_VALUES = "values";
public static final String COLUMN_TARGET = "target";
public static final String COLUMN_CURRENT = "current";

public MyDBHandler(Context context){

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

    String query1 = "CREATE TABLE " + TABLE_VALUES + " (" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLUMN_TARGET + " INT, " +
            COLUMN_CURRENT + " INT);";

    db.execSQL(query1);


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_VALUES);
    onCreate(db);
}


public void setTarget(int tar){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_TARGET, tar);
        values.put(COLUMN_CURRENT, 0);
        db.insert(TABLE_VALUES, null, values);
        db.close();

}


public int getTarget() {
   SQLiteDatabase db = this.getReadableDatabase();
   int targetVal=0;

String[] columns = {COLUMN_TARGET};
Cursor c = db.query(TABLE_VALUES, columns, null, null, null, null, String.valueOf(1));


if(c.getCount()>0)
    {
        c.moveToFirst();

        targetVal=c.getInt(0);
    }


   db.close();
    c.close();
    return targetVal;
}

}

这里用到了set函数:

public class home extends AppCompatActivity {

MyDBHandler db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    db = new MyDBHandler(this);

}

public void enter(View view)
{
    EditText target = (EditText) findViewById(R.id.target);
    TextView warning = (TextView) findViewById(R.id.warning);

    String check = target.getText().toString();
    if(check.equals("")) {
        // Intent a = new Intent(this, MainActivity.class);
        //startActivity(a);
        warning.setText("Please enter a value");
        return;
    }
    else {
        int input = Integer.parseInt(check);
        Log.d("tag", "111\n");
        db.setTarget(input);
        Log.d("tag", "222\n");
        //int i = db.getTarget();

        //Log.d("tag","input is " + i );


        Intent a = new Intent(this, MainActivity.class);
        startActivity(a);

    }




}


}

这里用到了get函数:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void onTrack(View view){
    Log.d("tag", "traaaack\n");
    Intent i = new Intent(this, Track.class);
    startActivity(i);
}

}

这是日志中的错误消息部分。

这是调用 set 方法的时间:

E/SQLiteLog: (1) “值”附近:语法错误 E/SQLiteDatabase:插入电流=0 目标=2000 时出错 android.database.sqlite.SQLiteException: near "values": syntax error (code 1): , while compile: INSERT INTO values(current,target) VALUES (?,?)

这是调用 get 方法的时间:

E/SQLiteLog: (1) “值”附近:语法错误 E/AndroidRuntime: 致命异常: main java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.user.calorietracker/com.example.user.calorietracker.Track}:android.database.sqlite.SQLiteException:接近“值”:语法错误(代码 1) : ,编译时:SELECT target FROM values LIMIT 1

我已经尝试了很多方法来修复它,但无法弄清楚我做错了什么。从字面上看,任何建议都会有所帮助!谢谢

【问题讨论】:

  • 将表名从values 更改为其他名称或使用双引号转义表名:"values"

标签: java android sql sqlite


【解决方案1】:

根据SQLite Keywords 的列表,values 这个词是您不能用作表(或列或许多其他东西)名称的词之一。

有几种方法可以解决这个问题:

  1. 不要使用这个词。例如,您始终可以使用tbl_values
  2. 你可以引用这个词:

'keyword'         单引号中的关键字是字符串文字。
"keyword"     双引号中的关键字是标识符。
[keyword]     方括号中的关键字是标识符。这不是标准 SQL。此引用机制由 MS Access 和 SQL Server 使用,并包含在 SQLite 中以实现兼容性。
`keyword`     用重音符号(ASCII 代码 96)括起来的关键字是一个标识符。这不是标准 SQL。这种引用机制由 MySQL 使用,并包含在 SQLite 中以实现兼容性。

【讨论】:

  • 不客气 :) 也将感谢您对答案的投票(左侧的箭头)。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多