SQLite数据库问题,真的不确定

时间:2016-04-19 17:58:56

标签: android sqlite

我试图使用SQLite创建一个简单的数据库,并在运行应用程序时遇到错误。该应用程序运行正常,直到我按下按钮将数据保存到数据库中,错误消息即时获取..

04-19 13:47:00.492 26807-26807/com.example.bash1.sqlitediss E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from GradienCache
04-19 13:47:00.500 26807-26807/com.example.bash1.sqlitediss E/OpenGLRenderer: MAX_TEXTURE_SIZE: 16384
04-19 13:47:00.520 26807-26807/com.example.bash1.sqlitediss E/OpenGLRenderer: Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
04-19 13:47:31.164 26807-26807/com.example.bash1.sqlitediss E/SQLiteLog: (1) near "tablestock_table": syntax error
04-19 13:47:31.164 26807-26807/com.example.bash1.sqlitediss E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bash1.sqlitediss, PID: 26807
android.database.sqlite.SQLiteException: near "tablestock_table": syntax error (code 1): , while compiling: create tablestock_table (IDNameDatereceivedExpirydate)

我的代码功能如下..

package com.example.bash1.sqlitediss;

import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_STOCKDB = "Stock.db";


public static final String DATABASE_STOCKDB = "Stock.db";
public static final String TABLE_NAME = "stock_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "Name";
public static final String COL_3 = "Datereceived";
public static final String COL_4 = "Expirydate";

public DatabaseHelper(Context context) {
    super(context, DATABASE_STOCKDB, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table" + TABLE_NAME + " (" + COL_1 + COL_2 + COL_3 + COL_4 + ")");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS TABLE_NAME" + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String name, String Datereceived, String expirydate){
SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, name);
    contentValues.put(COL_3, Datereceived);
    contentValues.put(COL_4, expirydate);
    long result = db.insert(TABLE_NAME,null,contentValues);
    if (result == -1 )
            return false;
    else
            return true;

}



}

2 个答案:

答案 0 :(得分:1)

 db.execSQL("create table "  + TABLE_NAME + " (" + COL_1 + COL_2 + COL_3 + COL_4 + ")");

在“创建表格”之后添加一个空格

答案 1 :(得分:1)

检查代码中的以下方法

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (" + COL_1 + " TEXT, " + COL_2 + " TEXT, " + COL_3 + " TEXT, " + COL_4 + " TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}
  • 在onCreate方法中,您没有在create tableTABLE_NAME之间添加空格。

  • 在onCreate方法中,您尚未指定列的数据类型。

  • 在onUpgrade方法中,您在查询中添加了TABLE_NAME变量TABLE_NAME

希望它能提供帮助。