0

我有以下代码从布局中的字段收集数据(同一类)并尝试将其放入 msqlite 数据库中:

public void submitOrder(Order order_) {
    SQLiteDatabase db=DBHelper.getWritableDatabase();
    ContentValues cv=new ContentValues();
        cv.put(DatabaseHelper.colName,      order_.name);
        cv.put(DatabaseHelper.colLink,      order_.link);
        cv.put(DatabaseHelper.colPrice,     order_.price);
        cv.put(DatabaseHelper.colDateYear,  order_.year);
        cv.put(DatabaseHelper.colDateMonth, order_.month);
        cv.put(DatabaseHelper.colDateDay,   order_.day);

        db.insert(DatabaseHelper.orderTable, null, cv);
    db.close();

我有我创建的 MSQlite 数据库助手类:

package com.Sagi.MyOrders;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

static final String dbName="DBname";
static final String orderTable="Orders";
static final String colName="OrderName";
static final String colLink="OrderLink";
static final String colDateYear="DateYear";
static final String colDateMonth="DateMonth";
static final String colDateDay="DateDay";
static final String colPrice="OrderPrice";
static String CREATE_T;

public DatabaseHelper(Context context) {
      super(context, dbName, null,1);
}
/** Called when the activity is first created. */
@Override
public void onCreate(SQLiteDatabase db) {
        CREATE_T="CREATE TABLE orderTable ("
                + "colName TEXT,"
                + "colLink TEXT,"
                + "colPrice TEXT,"
                + "colDateYear INTEGER,"
                + "colDateMonth INTEGER,"
                + "colDateDay INTEGER);";
        db.execSQL(CREATE_T);
        Log.e("Table creator","Table Created successfully");
     }

当我运行它并单击按钮将信息存储在数据库中时,我收到一条错误消息:

06-02 06:19:43.454: E/Database(1570): android.database.sqlite.SQLiteException: table Orders has no column named OrderPrice: , while compiling: INSERT INTO Orders(DateDay, DateMonth, OrderLink, OrderName, OrderPrice, DateYear) VALUES(?, ?, ?, ?, ?, ?);

还有一条 I 级消息:

06-02 06:19:43.444: I/Database(1570): sqlite returned: error code = 1, msg = table Orders has no column named OrderPrice

希望你能帮助...谢谢!

4

1 回答 1

1

它说table Orders has no column named OrderPrice。原因是您不更新数据库。只需卸载/重新安装您的应用程序。一切都会好起来的。

编辑后:

你创建你的表

CREATE_T="CREATE TABLE orderTable ("
                + "colName TEXT,"
                + "colLink TEXT,"
                + "colPrice TEXT,"
                + "colDateYear INTEGER,"
                + "colDateMonth INTEGER,"
                + "colDateDay INTEGER);";
        db.execSQL(CREATE_T);

这意味着您创建一个名为orderTable.

但在您的插入中:

public void submitOrder(Order order_) {
    SQLiteDatabase db=DBHelper.getWritableDatabase();
    ContentValues cv=new ContentValues();
        cv.put(DatabaseHelper.colName,      order_.name);
        cv.put(DatabaseHelper.colLink,      order_.link);
        cv.put(DatabaseHelper.colPrice,     order_.price);
        cv.put(DatabaseHelper.colDateYear,  order_.year);
        cv.put(DatabaseHelper.colDateMonth, order_.month);
        cv.put(DatabaseHelper.colDateDay,   order_.day);

        db.insert(DatabaseHelper.orderTable, null, cv); // This line
    db.close();

您使用DatabaseHelper.orderTablewhich isstatic final String orderTable="Orders"; 它们不相等,因此您收到此错误。

更改您的 orderTable 变量或表名,然后就可以了

希望这可以帮助

于 2012-06-02T06:42:25.030 回答