无法将项目添加到SQLite数据库Android Studio

时间:2016-10-20 12:03:39

标签: android sqlite

我真的不知道如何表达我的问题,但我创建了我的数据库处理程序类,但在我尝试插入项目时的活动中,它显示该列未找到。使用log,我注意到在插入时,它没有到达databasehandler类中的oncreate函数,所以我认为这可能是一个问题?这是我的代码:

public class OrderActivity extends AppCompatActivity {

private List<MyMenuItem> itemList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    DatabaseHandler db=DatabaseHandler.getInstance(getApplicationContext());



    db.addItem(new MyMenuItem(),2);
    RecyclerView rv = (RecyclerView) findViewById(R.id.rv_recycler_view);
    itemList=new ArrayList<>();
    itemList=db.getAllItems();
    rv.setHasFixedSize(true);
    OrderAdapter adapter = new OrderAdapter(getApplicationContext(),itemList);

    rv.setAdapter(adapter);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    rv.setLayoutManager(llm);


}

我的数据库类:

public class DatabaseHandler extends SQLiteOpenHelper {

private static DatabaseHandler _instance;

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "StocksManager";

// Contacts table name
private static final String TABLE_STOCK= "stock";

// Contacts Table Columns names
private static final String KEY_ID = "_id";
private static final String KEY_NAME = "Name";
private static final String KEY_PRICE= "Price";
private static final String KEY_IMAGE_URL="ImageURL";
private static final String KEY_DETAIL="Detail";
private static final String KEY_DISCOUNT="Discount";
private static final String KEY_QTY="Quantity";


public static synchronized DatabaseHandler getInstance(Context context)
{
    if(_instance==null)
    {
        _instance=new DatabaseHandler(context);
    }
    return _instance;
}

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    final String CREATE_STOCK_TABLE = "CREATE TABLE " + TABLE_STOCK
            + "("
            + KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_NAME + " TEXT,"
            + KEY_PRICE + "INTEGER"
            + KEY_IMAGE_URL  + "STRING"
            + KEY_DETAIL +" TEXT"
            + KEY_DISCOUNT + " INTEGER"
            + KEY_QTY+ " INTEGER"
            +")";
    db.execSQL(CREATE_STOCK_TABLE);
   // Log.d("Oncreate", "table created");
}

Logcat错误:

    10-19 16:12:39.367 5563-5563/com.frimondi.restaurant.restaurant E/SQLiteLog: (1) table stock has no column named Discount
    10-19 16:12:39.379 5563-5563/com.frimondi.restaurant.restaurant E/SQLiteDatabase: Error inserting Discount=0 Detail= _id=0 Price=1 ImageURL= Quantity=2 Name= android.database.sqlite.SQLiteException: table stock has no column named Discount (code 1): , while compiling: INSERT INTO stock(Discount,Detail,_id,Price,ImageURL,Quantity,Name) VALUES (?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.frimondi.restaurant.restaurant.DatabaseHandler.addItem(DatabaseHandler.java:106)

2 个答案:

答案 0 :(得分:0)

当数据库文件不存在时,

SQLiteOpenHelper onCreate()仅被调用一次。 When is SQLiteOpenHelper onCreate() / onUpgrade() run?

您的某些列中缺少空格和逗号:列名称及其类型之间需要空格,列规范之间需要逗号。添加这些内容后,您可以卸载应用以再次执行onCreate()

答案 1 :(得分:0)

您的数据库没有“折扣”列。 卸载您的应用程序并运行项目或 使用Sqlite Studio检查您的数据库。