在我的Android应用程序中:我所做的只是添加了一个名为“付款模式”的新列,我总是没有列名为付款模式的列。这有什么不对?
android.database.sqlite.SQLiteException: table order_details has no column named order_details_payment_mode (code 1):
我的表字段名称如下:我在用户名之前添加了付款方式。
private static final String TABLE_ORDER_DETAILS = "order_details";
private static final String KEY_ORDERSNO_LOCAL = "sno";
private static final String KEY_ORDER_DETAILS_ORDERID_LOCAL = "order_id";
private static final String KEY_ORDER_DETAILS_ID_LOCAL = "order_details_productid";
private static final String KEY_ORDER_DETAILS_NAME_LOCAL = "order_details_productname";
private static final String KEY_ORDER_DETAILS_IMAGE_LOCAL = "order_details_productimage";
private static final String KEY_ORDER_DETAILS_PRICE_LOCAL= "order_details_productprice";
private static final String KEY_ORDER_DETAILS_PLAN_LOCAL= "order_details_productplan";
private static final String KEY_ORDER_DETAILS_QTYPERDAY_LOCAL= "order_details_qtyperday";
private static final String KEY_ORDER_DETAILS_TOTALQTY_LOCAL= "order_details_totalqty";
private static final String KEY_ORDER_DETAILS_REMAINQTY_LOCAL= "order_details_remainqty";
private static final String KEY_ORDER_DETAILS_TOTALPRICE= "order_details_totalamount";
private static final String KEY_ORDER_DETAILS_STARTDATE_LOCAL= "order_details_startdate";
private static final String KEY_ORDER_DETAILS_STATUS= "order_details_status";
private static final String KEY_ORDER_DETAILS_ADHOC_START_LOCAL= "order_details_adhoc_start";
private static final String KEY_ORDER_DETAILS_ADHOC_QTY_LOCAL= "order_details_adhoc_qtyperday";
private static final String KEY_ORDER_DETAILS_PAUSE_LOCAL= "order_details_pause";
private static final String KEY_ORDER_DETAILS_PAUSESTARTDATE_LOCAL= "order_details_pausestartdate";
private static final String KEY_ORDER_DETAILS_PAUSEENDDATE_LOCAL= "order_details_pauseenddate";
private static final String KEY_ORDER_DETAILS_TRANSACTIONORDERID_LOCAL= "order_details_transactionorderid";
private static final String KEY_ORDER_DETAILS_PAYMENT_MODE_LOCAL = "order_details_payment_mode";
private static final String KEY_ORDER_DETAILS_USERNAME_LOCAL= "order_details_username";
这是创建表的代码。
String CREATE_TABLE_ORDER_DETAILS = "CREATE TABLE IF NOT EXISTS " + TABLE_ORDER_DETAILS + "("
+ KEY_ORDERSNO_LOCAL + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_ORDER_DETAILS_ORDERID_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_ID_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_NAME_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_IMAGE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PRICE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PLAN_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_QTYPERDAY_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_TOTALQTY_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_REMAINQTY_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_TOTALPRICE + " TEXT,"
+ KEY_ORDER_DETAILS_STARTDATE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_STATUS + " TEXT,"
+ KEY_ORDER_DETAILS_ADHOC_START_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_ADHOC_QTY_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PAUSE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PAUSESTARTDATE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PAUSEENDDATE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_TRANSACTIONORDERID_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PAYMENT_MODE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_USERNAME_LOCAL + " TEXT"
+ ")";
db.execSQL(CREATE_TABLE_ORDER_DETAILS);
09-11 07:20:23.641 17703-17703/com.ylg.orderapp E/SQLiteLog: (1) table order_details has no column named order_details_payment_mode
09-11 07:20:23.646 17703-17703/com.ylg.orderapp E/SQLiteDatabase: Error inserting order_details_totalqty=70 order_details_productplan=7 order_details_pause=0 order_details_productimage=
order_details_productname=1 litre bottle order_details_productprice=90 order_id=1 order_details_remainqty=0 order_details_status=CANCEL order_details_username=+441234567890 order_details_qtyperday=10 order_details_productid=1 order_details_totalamount=6300.0 order_details_pauseenddate=0 order_details_startdate=16/8/2018 order_details_pausestartdate=0 order_details_payment_mode=PPI order_details_transactionorderid=123456789015224 order_details_adhoc_start=0 order_details_adhoc_qtyperday=0
android.database.sqlite.SQLiteException: table order_details has no column named order_details_payment_mode (code 1): , while compiling: INSERT INTO order_details(order_details_totalqty,order_details_productplan,order_details_pause,order_details_productimage,order_details_productname,order_details_productprice,order_id,order_details_remainqty,order_details_status,order_details_username,order_details_qtyperday,order_details_productid,order_details_totalamount,order_details_pauseenddate,order_details_startdate,order_details_pausestartdate,order_details_payment_mode,order_details_transactionorderid,order_details_adhoc_start,order_details_adhoc_qtyperday) 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.ylg.db.OrderDB.setorderdetails(OrderDB.java:1514)
at com.ylg.orderapp.ProductDetailsList$5.onSuccess(ProductDetailsList.java:661)
at com.loopj.android.http.JsonHttpResponseHandler$1$1.run(JsonHttpResponseHandler.java:152)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
查询保存和更新订购详情:
https://textuploader.com/dv14k
更新:根据MikeT建议我记录了createTable以查看发生了什么:下面是文本:
CREATE TABLE IF NOT EXISTS order_details(sno INTEGER PRIMARY KEY AUTOINCREMENT,order_id TEXT,order_details_productid TEXT,order_details_productname TEXT,order_details_productimage TEXT,order_details_productprice TEXT,order_details_productplan TEXT,order_details_qtyperday TEXT,order_details_totalqty TEXT,order_details_remainqty TEXT,order_details_totalamount TEXT,order_details_startdate TEXT,order_details_status TEXT,order_details_adhoc_start TEXT,order_details_adhoc_qtyperday TEXT,order_details_pause TEXT,order_details_pausestartdate TEXT,order_details_pauseenddate TEXT,order_details_transactionorderid TEXT,order_details_payment_mode TEXT,order_details_username TEXT)
更新2:活动中使用的查询:
if(logdb.checkOrderID(orderID) == 0)
{ logdb.setorderdetails(orderID, orderProductID, orderProductNAME, orderProductIMAGE, orderProductPRICE, orderProductPLAN, orderProductPERDAY, orderProductTOTALQTY, orderProductRemainQTY, orderProductTOTALAMOUNT, orderProductSTARTDATE,orderProductSTATUS, orderProductADHOCSTART, orderProductADHOCQTY, orderProductPAUSED, orderProductPAUSEDSTART, orderProductPAUSEDEND, orderProductTransactionOrderID, orderProductPaymentType, orderProductUSER);
}
else
{
logdb.updateorderdetails(orderID, orderProductID, orderProductNAME, orderProductIMAGE, orderProductPRICE, orderProductPLAN, orderProductPERDAY, orderProductTOTALQTY, orderProductRemainQTY, orderProductTOTALAMOUNT, orderProductSTARTDATE, orderProductSTATUS, orderProductADHOCSTART, orderProductADHOCQTY, orderProductPAUSED, orderProductPAUSEDSTART, orderProductPAUSEDEND, orderProductTransactionOrderID, orderProductPaymentType, orderProductUSER); }
所以我已经清除缓存,应用程序数据,卸载,更改版本,更改应用程序版本,清除工作室中的缓存但仍然相同。
我无法在这里找出语法错误。
可能有人可以帮我找到错误?
谢谢!
复制您提供的代码并假设SQLite数据库的相对标准编码(即使用SQLiteOpenhelper的子类),那么您的代码似乎没有任何问题。
请考虑以下代码 - 首先注意将order_details_payment_mode列注释掉 - 上面的代码按原样复制。
DatabaseHelper.java(SQLiteOpenhelper的子类): -
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String TABLE_ORDER_DETAILS = "order_details";
private static final String KEY_ORDERSNO_LOCAL = "sno";
private static final String KEY_ORDER_DETAILS_ORDERID_LOCAL = "order_id";
private static final String KEY_ORDER_DETAILS_ID_LOCAL = "order_details_productid";
private static final String KEY_ORDER_DETAILS_NAME_LOCAL = "order_details_productname";
private static final String KEY_ORDER_DETAILS_IMAGE_LOCAL = "order_details_productimage";
private static final String KEY_ORDER_DETAILS_PRICE_LOCAL= "order_details_productprice";
private static final String KEY_ORDER_DETAILS_PLAN_LOCAL= "order_details_productplan";
private static final String KEY_ORDER_DETAILS_QTYPERDAY_LOCAL= "order_details_qtyperday";
private static final String KEY_ORDER_DETAILS_TOTALQTY_LOCAL= "order_details_totalqty";
private static final String KEY_ORDER_DETAILS_REMAINQTY_LOCAL= "order_details_remainqty";
private static final String KEY_ORDER_DETAILS_TOTALPRICE= "order_details_totalamount";
private static final String KEY_ORDER_DETAILS_STARTDATE_LOCAL= "order_details_startdate";
private static final String KEY_ORDER_DETAILS_STATUS= "order_details_status";
private static final String KEY_ORDER_DETAILS_ADHOC_START_LOCAL= "order_details_adhoc_start";
private static final String KEY_ORDER_DETAILS_ADHOC_QTY_LOCAL= "order_details_adhoc_qtyperday";
private static final String KEY_ORDER_DETAILS_PAUSE_LOCAL= "order_details_pause";
private static final String KEY_ORDER_DETAILS_PAUSESTARTDATE_LOCAL= "order_details_pausestartdate";
private static final String KEY_ORDER_DETAILS_PAUSEENDDATE_LOCAL= "order_details_pauseenddate";
private static final String KEY_ORDER_DETAILS_TRANSACTIONORDERID_LOCAL= "order_details_transactionorderid";
private static final String KEY_ORDER_DETAILS_PAYMENT_MODE_LOCAL = "order_details_payment_mode";
private static final String KEY_ORDER_DETAILS_USERNAME_LOCAL= "order_details_username";
String CREATE_TABLE_ORDER_DETAILS = "CREATE TABLE IF NOT EXISTS " + TABLE_ORDER_DETAILS + "("
+ KEY_ORDERSNO_LOCAL + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_ORDER_DETAILS_ORDERID_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_ID_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_NAME_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_IMAGE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PRICE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PLAN_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_QTYPERDAY_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_TOTALQTY_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_REMAINQTY_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_TOTALPRICE + " TEXT,"
+ KEY_ORDER_DETAILS_STARTDATE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_STATUS + " TEXT,"
+ KEY_ORDER_DETAILS_ADHOC_START_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_ADHOC_QTY_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PAUSE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PAUSESTARTDATE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_PAUSEENDDATE_LOCAL + " TEXT,"
+ KEY_ORDER_DETAILS_TRANSACTIONORDERID_LOCAL + " TEXT,"
// + KEY_ORDER_DETAILS_PAYMENT_MODE_LOCAL + " TEXT," //<<<<<< NEW COLUMN
+ KEY_ORDER_DETAILS_USERNAME_LOCAL + " TEXT"
+ ")";
public static final String DATABASE_NAME = "mydb";
public static final int DATABASE_VERSION = 1;
public static SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
Log.d("CRTORDERDETAILS",CREATE_TABLE_ORDER_DETAILS);
sqLiteDatabase.execSQL(CREATE_TABLE_ORDER_DETAILS);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
Splash.java(使用/访问数据库的活动): -
public class Splash extends AppCompatActivity {
DatabaseHelper databaseHelper;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
databaseHelper = new DatabaseHelper(this);
String sqlstmnt = "SELECT order_details_payment_mode FROM order_details;";
//Cursor csr2 = getDatabase().query("order_details",new String[]{"order_details_payment_mode"},null,null,null,null,null);
Log.d("ORDER_DETAILS SQL","SQL for query is " + sqlstmnt);
Cursor csr2 = databaseHelper.getWritableDatabase().rawQuery(sqlstmnt,null);
String[] columns = csr2.getColumnNames();
for (String s: columns) {
Log.d("ORDER_DETAILS COLS","Column name from cursor = " + s);
}
csr2.close();
}
}
所以这个活动: -
这失败了,因为order_details_payment_mode列故意没有包含在与您类似的错误中: -
09-11 02:00:31.414 1777-1777/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{so52226238.so52226238/so52226238.so52226238.Splash}: android.database.sqlite.SQLiteException: no such column: order_details_payment_mode (code 1): , while compiling: SELECT order_details_payment_mode FROM order_details;
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such column: order_details_payment_mode (code 1): , while compiling: SELECT order_details_payment_mode FROM order_details;
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
at so52226238.so52226238.Splash.onCreate(Splash.java:20)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
线at so52226238.so52226238.Splash.onCreate(Splash.java:20)
指向
Cursor csr2 = databaseHelper.getWritableDatabase().rawQuery(sqlstmnt,null);
字符串sqlstmnt使用: - 设置
String sqlstmnt = "SELECT order_details_payment_mode FROM order_details;";
预计这将失败,因为显然该列不存在。
删除App的数据并取消注释掉DatabaseHelper.java中order_details_payment_mode列的行,然后使用以下输出运行ok到日志: -
09-11 02:20:12.834 1897-1897/? D/dalvikvm: VFY: replacing opcode 0x6f at 0x0004
09-11 02:20:12.842 1897-1897/? D/CRTORDERDETAILS: CREATE TABLE IF NOT EXISTS order_details(sno INTEGER PRIMARY KEY AUTOINCREMENT,order_id TEXT,order_details_productid TEXT,order_details_productname TEXT,order_details_productimage TEXT,order_details_productprice TEXT,order_details_productplan TEXT,order_details_qtyperday TEXT,order_details_totalqty TEXT,order_details_remainqty TEXT,order_details_totalamount TEXT,order_details_startdate TEXT,order_details_status TEXT,order_details_adhoc_start TEXT,order_details_adhoc_qtyperday TEXT,order_details_pause TEXT,order_details_pausestartdate TEXT,order_details_pauseenddate TEXT,order_details_transactionorderid TEXT,order_details_payment_mode TEXT,order_details_username TEXT)
09-11 02:20:12.846 1897-1897/? D/ORDER_DETAILS SQL: SQL for query is SELECT order_details_payment_mode FROM order_details;
09-11 02:20:12.846 1897-1897/? D/ORDER_DETAILS COLS: Column name from cursor = order_details_payment_mode
我刚刚通过添加新字段检查了其他表,除了order_details(相同代码)之外,所有内容都被创建。完全令人惊讶。
然后看起来IF NOT EXISTS
导致noop,因为在此之前创建了表。或者表后面有DROP和CREATE。
如果你要改变
db.execSQL(CREATE_TABLE_ORDER_DETAILS);
至 :-
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ORDER_DETAILS + ")";
db.execSQL(CREATE_TABLE_ORDER_DETAILS);
如果它然后工作,这将表明该表正在创建之前,因此IF NOT EXISTS导致noop。
如果它仍然因同一问题而失败,那么表格可能会被删除并在上述之后重新创建。