android.database.sqlite.SQLiteException运行我的应用程序

时间:2015-06-09 12:24:41

标签: android sqlite

数据库类

public class DBhandle {

private static final String DATABASE_NAME = "restaurantdatabase";
private static final int DATABASE_VERSION = 1;
final Context context;
private SQLiteDatabase ourDatabase;
DatabaseHelper dbHelper;


//table name
private static final String CUSTINFO_TABLE_NAME= "Custinfo";

//login table column name
    public final static String C_ID = "_id";
    public final static String C_NAME = "cust_name";
    public final static String C_PHONE = "cust_phone";
    public final static String C_EMAIL = "cust_email";
    public final static String C_ADDR = "cust_address";
   public class DatabaseHelper extends SQLiteOpenHelper{

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

    }

   db.execSQL("CREATE TABLE " + CUSTINFO_TABLE_NAME + " (" + C_ID 
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + C_NAME 
                + " TEXT NOT NULL, " + C_PHONE + " TEXT NOT NULL, " + C_EMAIL 
                + " TEXT NOT NULL, " + C_ADDR + " TEXT NOT NULL);"
                );

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + CUSTINFO_TABLE_NAME);
        onCreate(db);
        }

public long addCustInfo(String custname, String custno, String custemail,String custaddress) {

    ContentValues newValues = new ContentValues();
    // Assign values for each row.
    newValues.put("cust_name", custname);
    newValues.put("cust_phone", custno);
    newValues.put("cust_email", custemail);
    newValues.put("cust_address", custaddress);

    // Insert the row into your table
    return ourDatabase.insert(CUSTINFO_TABLE_NAME, null, newValues);
}


### data class ###
public void onClick(View arg0) {
            //Intent saveintent=new Intent(getApplicationContext(),)

            String custname=edittextcust_name.getText().toString();
            String custno=edittextcust_no.getText().toString();
            String custemail=edittextcust_email.getText().toString();
            String custaddress=edittextcust_address.getText().toString();

            if(custname.equals("")||custno.equals("")||custemail.equals("")||custaddress.equals(""))
            {
                Toast.makeText(Custentry.this, "field vacant", Toast.LENGTH_LONG).show();

            }
            else
            {
                dbhandle.addCustInfo(custname, custno, custemail, custaddress);
                Toast.makeText(Custentry.this, " welcome" +custname, Toast.LENGTH_LONG).show();
            }

当我运行我的应用程序时,它会崩溃并显示此消息" android.database.sqlite.SQLiteException:没有这样的表:CUSTINFO_TABLE_NAME(代码1):,同时编译:INSERT INTO CUSTINFO_TABLE_NAME(cust_phone,cust_name,cust_address,cust_email )VALUES(?,?,?,?)"`

3 个答案:

答案 0 :(得分:2)

您的表格名称为Custinfo而不是CUSTINFO_TABLE_NAMECUSTINFO_TABLE_NAME是一个保存表名的变量。

基本上,在代码中的某个位置,如果没有双引号,则"CUSTINFO_TABLE_NAME"代替CUSTINFO_TABLE_NAME。您发布的代码并未显示。

答案 1 :(得分:1)

卸载应用程序completelty并再次运行。您可以转到设置>卸载应用。应用>点击您要卸载的应用>卸载,无论是在手机上还是在AVD上运行 有时这个问题也会出现。

答案 2 :(得分:0)

错误消息显示,它无法插入表“CUSTINFO_TABLE_NAME”,而您的表名为“Custinfo”(持有它的变量是CUSTINFO_TABLE_NAME)。您在此处发布的代码似乎没有问题,尝试检查您的insert语句然后您可以尝试将insert语句更改为

return ourDatabase.insert("Custinfo", null, newValues);

看看会发生什么......