输入数据的SQLite数据库错误

时间:2016-04-19 19:15:01

标签: android sqlite

我差不多已经完成了我的第一个数据库的创建,但我认为oncreate中的一些语法是错误的。我无法弄清楚在哪里?我已经尝试过查看教程并试图为我们的代码定制代码,但似乎没有任何工作。

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 {
// Database name
public static final String DATABASE_STOCKDB = "Stock.db";
// Column names
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 stock_table " + TABLE_NAME + " (" + COL_1 + "INTEGER PRIMARY KEY,"+ COL_2 + "INTEGER,"+ COL_3 + "INTEGER," + COL_4 +"INTEGER" + ")");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + 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;

    }  
}

错误如下:

  

插入时出错Name = sss Datereceived = s Expirydate = s`

     

android.database.sqlite.SQLiteException:table stock_table没有名为Name(code 1)的列:,编译时:INSERT INTO stock_table(Name,Datereceived,Expirydate)VALUES(?,?,?)

2 个答案:

答案 0 :(得分:0)

您有"create table stock_table" + TABLE_NAME - 您正在复制表名。您的列名称与其类型之间也没有空格。为每个列尝试COL_1 + " INTEGER..."

另外,检查传递给insertData的值。您传入String,但将列定义为INTEGER。你确定你不想要列的TEXT吗?

请查看this以获取进一步指导。

此代码对我来说正常工作:

public class DatabaseHelper extends SQLiteOpenHelper {
  // Database name
  public static final String DATABASE_STOCKDB = "Stock.db";
  // Column names
  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 + " INTEGER PRIMARY KEY,"+ 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);
  }

  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;

  }

}

如果您运行应用程序然后更改了数据库设计/架构并且未正确增加版本号,则数据库可能处于不一致状态。尝试完全卸载应用程序并重新安装。

答案 1 :(得分:0)

问题是你的create table命令在列名和类型之间没有空格 -

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

这就是为什么你没有专栏"姓名"但是" NameINTEGER"。类似的问题也与其他专栏有关。此外,您正在插入文本值但是您具有整数类型列,因此将除主键之外的列更改为文本类型列。将其更改为 -

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