java - 在 android sqlite 数据库中插入和执行查询时出错

标签 java android sql sqlite

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

这是我的代码:

公共(public)类 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) near "values": 语法错误 E/SQLiteDatabase: 错误插入 current=0 target=2000 android.database.sqlite.SQLiteException: near "values": syntax error (code 1): ,编译时:INSERT INTO values(current,target) VALUES (?,?)

这是在调用 get 方法时:

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

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

最佳答案

根据列表SQLite Keywords - 词 values 是不能用作表名(或列或许多其他名称)的词之一。

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

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

'keyword'         A keyword in single quotes is a string literal.
"keyword"       A keyword in double-quotes is an identifier.
[keyword]       A keyword enclosed in square brackets is an identifier. This is not standard SQL. This quoting mechanism is used by MS Access and SQL Server and is included in SQLite for compatibility.
`keyword`       A keyword enclosed in grave accents (ASCII code 96) is an identifier. This is not standard SQL. This quoting mechanism is used by MySQL and is included in SQLite for compatibility.

关于java - 在 android sqlite 数据库中插入和执行查询时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41309057/

相关文章:

php - 无法使用 PHP/PDO 在 mySQL DB 上更新/插入

c# - SQL 插入导致空字段

java - Java 解释器或任何解释器到底是如何工作的?

java - Hibernate JPAModelGen 5.4.6.Final 在 JDK11 上失败

java - 如何将复制构造函数作为方法引用传递?

android - Firebase 存储和 Android 图像

android - 如何使用 ActiveAndroid 读写外键?

android Tablelayout 出屏

java - 通过路径将带有数组的键添加到json

mysql - Oracle - 返回具有 max_date 重复项的行