表tablename没有名为columnname的列(代码1)

时间:2018-09-11 00:50:28

标签: android android-sqlite

在我的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); }

所以我已经清除了缓存,应用程序数据,已卸载,更改版本,更改了应用程序版本,清除了Studio中的缓存,但仍然保持不变。

在这里我无法弄清语法错误。

也许有人可以帮助我找到错误?

谢谢!

1 个答案:

答案 0 :(得分:0)

复制您提供的代码,并假设使用相对标准的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();
    }
}

所以这个活动:-

  • 实例化DatabaseHelper的一个实例(以访问数据库)。
  • 运行查询以获取 order_details 表中的所有行 order_details_payment_mode

首次运行,

此操作失败,因为有意未包含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;";

由于该列显然不存在,因此预期会失败。

第二次奔跑

删除应用程序的数据并取消注释 order_details_payment_mode 列的 DatabaseHelper.java 中的行,然后运行正常,并在日志中显示以下输出:-< / p>

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。

如果仍然由于相同的问题而失败,则可能是由于上述原因导致表已被删除并重新创建。