【问题标题】:SQLiteException no such table:ItemTable while compiling: Select_id,.... from ItemTableSQLiteException no such table:ItemTable while compile: Select_id,.... from ItemTable
【发布时间】:2016-02-08 22:41:53
【问题描述】:

我的 logcat 中出现此错误已有一段时间了。我尝试了很多事情,比如清除我的应用程序中的数据,卸载并重新安装我的应用程序,升级我的数据库版本,我什至删除了我的项目并创建了一个新项目,但是每次我运行我的应用程序时,我总是会收到这个错误.谁能帮帮我。

我想要做的是我在一个活动中有两个按钮添加项目和查看项目,每次我单击查看项目按钮时都会收到此错误。

单击视图按钮时,它必须在列表视图中显示我保存在数据库中的数据,但这不会发生,因为我的应用程序总是停止并且这会显示在我的 logcat 中。

This is the Image of the activity where the data should display

这是我的日志:

02-09 05:24:23.850: E/AndroidRuntime(8037):     at com.system.inventorysystem.ViewProductItem.onCreate(ViewProductItem.java:46)
02-09 05:46:59.810: E/AndroidRuntime(13150): FATAL EXCEPTION: main
02-09 05:46:59.810: E/AndroidRuntime(13150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.system.inventorysystem/com.system.inventorysystem.ViewProductItem}: android.database.sqlite.SQLiteException: no such table: ItemTable: , while compiling: SELECT _id, ItemDescription, ItemQuantity, ItemUnit, ItemPrice FROM ItemTable
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.os.Looper.loop(Looper.java:137)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.app.ActivityThread.main(ActivityThread.java:4340)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at java.lang.reflect.Method.invokeNative(Native Method)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at java.lang.reflect.Method.invoke(Method.java:511)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at dalvik.system.NativeStart.main(Native Method)
02-09 05:46:59.810: E/AndroidRuntime(13150): Caused by: android.database.sqlite.SQLiteException: no such table: ItemTable: , while compiling: SELECT _id, ItemDescription, ItemQuantity, ItemUnit, ItemPrice FROM ItemTable
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at com.system.inventorysystem.DBAdapter.getAllItemProduct(DBAdapter.java:108)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at com.system.inventorysystem.ViewProductItem.onCreate(ViewProductItem.java:48)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.app.Activity.performCreate(Activity.java:4465)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-09 05:46:59.810: E/AndroidRuntime(13150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-09 05:46:59.810: E/AndroidRuntime(13150):    ... 11 more

这是我的数据库适配器。

BDAdapter.java

package com.system.inventorysystem;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {

private static final String DATABASE_NAME = "ProductItemdb";
private static final int DATABASE_VERSION = 4;

private static final String TABLE_PRODUCTITEM = "ItemTable";
public static final String KEY_ItemID = "_id";
public static final String KEY_ITEM_DESCRIPTION = "ItemDescription";
public static final String KEY_ITEM_QUANTITY = "ItemQuantity";
public static final String KEY_ITEM_UNIT = "ItemUnit";
public static final String KEY_ITEM_PRICE = "ItemPrice";
public static final String KEY_ITEM_DATE = "Date/Time";
private static final String PRODUCTITEM_TABLE = "create table ItemTable (_id integer primary key autoincrement, "
        + " ItemDescription text not null, ItemQuantity integer not null, ItemUnit text not null, ItemPrice float not null, Date/Time datetime DEFAULT CURRENT_TIMESTAMP);";

private final Context ourContext;
private DatabaseHelper ourHelper;
private SQLiteDatabase ourDatabase;

public DBAdapter(Context con) {
    this.ourContext = con;
    ourHelper = new DatabaseHelper(ourContext);
}

static class DatabaseHelper extends SQLiteOpenHelper { 
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try {
        db.execSQL(PRODUCTITEM_TABLE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTITEM);
        onCreate(db);
    }
}

//open Database
public DBAdapter open() throws SQLException {
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
//close database
public void close() {
    ourHelper.close();
}

//METHODS FOR ITEM PRODUCTS
//insert data
public long addItemProduct(String description, int quantity, String unit, Float price)
{
    ContentValues cv = new ContentValues();
    cv.put(KEY_ITEM_DESCRIPTION, description);
    cv.put(KEY_ITEM_QUANTITY, quantity);
    cv.put(KEY_ITEM_UNIT, unit);
    cv.put(KEY_ITEM_PRICE, price);
    return ourDatabase.insert(TABLE_PRODUCTITEM, null, cv);
}
//update data
public boolean updateItemProduct(long itemId, String description, int quantity, String unit, Float price)
{
    ContentValues cv = new ContentValues();
    cv.put(KEY_ITEM_DESCRIPTION, itemId);
    cv.put(KEY_ITEM_QUANTITY, quantity);
    cv.put(KEY_ITEM_UNIT, unit);
    cv.put(KEY_ITEM_PRICE, price);
    return ourDatabase.update(TABLE_PRODUCTITEM, cv, KEY_ItemID + "=" + itemId, null) > 0;
}
//delete data
public boolean deleteItemProduct(long itemId)
{
    return ourDatabase.delete(TABLE_PRODUCTITEM, KEY_ItemID + "=" + itemId, null) > 0;
}
//retrieve data
public Cursor getItemProduct(long itemId) throws SQLException
{   
    Cursor mCursor = ourDatabase.query(true, TABLE_PRODUCTITEM, new String[] {KEY_ItemID, KEY_ITEM_DESCRIPTION, 
            KEY_ITEM_QUANTITY, KEY_ITEM_UNIT, KEY_ITEM_PRICE}, KEY_ItemID + "=" + itemId, null,
            null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}
//retrieve all data
public Cursor getAllItemProduct()
{
    return ourDatabase.query(TABLE_PRODUCTITEM, new String[] {KEY_ItemID, KEY_ITEM_DESCRIPTION, KEY_ITEM_QUANTITY, KEY_ITEM_UNIT, KEY_ITEM_PRICE}, 
            null, null, null, null, null);
}
}

这是我的 java 类,我试图在我的数据库中的列表视图中显示我的数据。

ViewItemProduct.java

package com.system.inventorysystem;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ViewProductItem extends Activity implements View.OnClickListener{

DBAdapter ourDatabase;
Typeface customFont;
TextView view_item_txt;
Button delete,update;
ListView myList;

@SuppressLint("NewApi") @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewproductitem);

    customFont = Typeface.createFromAsset(getAssets(), "fonts/EraserRegular.ttf");

    view_item_txt = (TextView)findViewById(R.id.view_item_tv);
    view_item_txt.setTypeface(customFont);

    delete = (Button)findViewById(R.id.view_delete_button);
    delete.setTypeface(customFont);
    delete.setOnClickListener(this);

    update = (Button)findViewById(R.id.view_update_button);
    update.setTypeface(customFont);
    update.setOnClickListener(this);

    openDB();
    ourDatabase.open();

    final Cursor c = ourDatabase.getAllItemProduct();
    String[] ViewItemsNames = new String[] {DBAdapter.KEY_ItemID,DBAdapter.KEY_ITEM_DESCRIPTION,DBAdapter.KEY_ITEM_QUANTITY,DBAdapter.KEY_ITEM_UNIT,DBAdapter.KEY_ITEM_PRICE};
    int[] ViewItemsID = new int[] {R.id.view_item_id,R.id.view_item_desc,R.id.view_item_quantity,R.id.view_item_unit,R.id.view_item_price};
    final SimpleCursorAdapter myCursorAdapter;
    myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),R.layout.viewproduct_items, c, ViewItemsNames, ViewItemsID,0);
    myList = (ListView)findViewById(R.id.view_item_listview);
    myList.setAdapter(myCursorAdapter);
}

private void openDB() {
    ourDatabase = new DBAdapter(this);
    ourDatabase.open();
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.view_delete_button:
        Intent openDeleteItem = new Intent(this,DeleteProductItem.class);
        this.startActivity(openDeleteItem);
        break;
    case R.id.view_update_button:
        Intent openUpdateItem = new Intent(this,UpdateProductItem.class);
        this.startActivity(openUpdateItem);
        break;
    }
}
}

这是我的 xml 文件。

查看itemproduct.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/greenboard_background" >

<TextView
    android:id="@+id/view_item_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="45dp"
    android:text="@string/view_item"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="35sp" />

<ListView
    android:id="@+id/view_item_listview"
    android:layout_width="543dp"
    android:layout_height="695dp"
    android:layout_below="@+id/view_item_tv"
    android:layout_centerHorizontal="true" >
</ListView>

<Button
    android:id="@+id/view_delete_button"
    android:layout_width="260dp"
    android:layout_height="70dp"
    android:layout_alignRight="@+id/view_item_listview"
    android:layout_below="@+id/view_item_listview"
    android:layout_marginTop="10dp"
    android:text="@string/delete_item"
    android:textSize="40sp" />

<Button
    android:id="@+id/view_update_button"
    android:layout_width="260dp"
    android:layout_height="70dp"
    android:layout_alignBaseline="@+id/view_delete_button"
    android:layout_alignBottom="@+id/view_delete_button"
    android:layout_alignLeft="@+id/view_item_listview"
    android:text="@string/update_item"
    android:textSize="40sp" />

</RelativeLayout>

这是我的列表视图中项目的 texviews。

查看product_items.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/view_item_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:layout_alignParentTop="true"
    android:text="@string/itemno"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30sp" />

<TextView
    android:id="@+id/view_item_desc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/item_desc"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30sp" />

<TextView
    android:id="@+id/view_item_quantity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:layout_below="@+id/view_item_desc"
    android:text="@string/item_quantity"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30sp" />

<TextView
    android:id="@+id/view_item_unit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/view_item_quantity"
    android:layout_alignBottom="@+id/view_item_quantity"
    android:layout_centerHorizontal="true"
    android:text="@string/item_unit"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30sp" />

<TextView
    android:id="@+id/view_item_price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:layout_below="@+id/view_item_unit"
    android:text="@string/item_price"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="30sp" />

</RelativeLayout>

【问题讨论】:

    标签: java android listview android-sqlite


    【解决方案1】:

    您的create table 中有语法问题。 Date/Time 不是有效的列名 - 去掉 /

    由于SQLiteOpenHelperonCreate() 中的try-catch,语法问题被隐藏。如果有问题,onCreate() 必须抛出异常。否则SQLiteOpenHelper 认为一切正常。

    解决上述两个问题后,卸载您的应用/清除其数据或提高数据库版本,以便再次调用onCreate()

    【讨论】:

    • 谢谢你,我可以问更多吗?我的一项活动有问题。我想要做的是在我的数据库中插入一个价格,例如(10.00),但是当我试图检索数据时,它只是像这样(10)返回它而不带小数点。你能帮助我吗? @laalto
    • 如果您有新问题,请将其作为新问题发布,而不是在 cmets 中扩展旧问题。
    【解决方案2】:

    您的 create table 语句格式不正确。

    表格列名中不允许有特殊符号

    因此,请从表格列名称中的日期/时间中删除“/”。

    它会解决问题。

    实际问题是由于这个问题没有创建表,并且由于 try catch 块而无法抛出异常。

    【讨论】:

    • 谢谢你,我可以问更多吗?我的一项活动有问题。我想要做的是在我的数据库中插入一个价格,例如(10.00),但是当我试图检索数据时,它只是像这样(10)返回它而不带小数点。你能帮我吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 2023-02-06
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多