mardi 16 juin 2015

android.database.sqlite.SQLiteException: no such table: CUSTINFO_TABLE_NAME (code 1): , while compiling: INSERT INTO [duplicate]

This question already has an answer here:

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 LOGIN_TABLE_NAME = "Login";
    private static final String CUSTINFO_TABLE_NAME = "Custinfo";

    //login table column name
    public final static String KEY_ROWID= "ID";
    public final static String A_NAME = "admin_name";
    public final static String A_PHONE = "admin_phone";

    //custinfo table column name
    public static final String C_ID = "_id";
    public static final String C_NAME = "cust_name";
    public static final String C_PHONE = "cust_phone";
    public static final String C_EMAIL = "cust_email";
    public static final String C_ADDR = "cust_address";

public class DatabaseHelper extends SQLiteOpenHelper{

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

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + LOGIN_TABLE_NAME + " (" + KEY_ROWID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + A_NAME
                + " TEXT NOT NULL, " + A_PHONE + " TEXT NOT NULL);"
                );

        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);"
                );

    }

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

        onCreate(db);

    }

}

public DBhandle(Context ctx){

        context = ctx;
}

public DBhandle open() throws SQLException
{
    dbHelper = new DatabaseHelper(context);
    ourDatabase = dbHelper.getWritableDatabase();

    return this;
}

public void close(){

    dbHelper.close();
}

public long insertEntry(String userName,String password)
{
    //SQLiteDatabase db = this.getWritableDatabase();
   ContentValues newValues = new ContentValues();
    // Assign values for each row.
    newValues.put("admin_name", userName);
    newValues.put("admin_phone", password);


    // Insert the row into your table
    return ourDatabase.insert(LOGIN_TABLE_NAME, null, newValues);
    //Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();

}

public String getSingleEntry(String userName) {
    Cursor cursor=ourDatabase.query(LOGIN_TABLE_NAME, null, " admin_name=?", new String[]{userName}, null, null, null);
    if(cursor.getCount()<1) // UserName Not ExistD
    {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String password= cursor.getString(cursor.getColumnIndex("admin_phone"));
    cursor.close();
    return password;

}

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);
}

}

custinfo class

public class Custentry extends ActionBarActivity {

EditText edittextcust_name,edittextcust_no,edittextcust_email,edittextcust_address;
Button save;
DBhandle dbhandle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.custentry);

    dbhandle=new DBhandle(this);
    dbhandle=dbhandle.open();

    edittextcust_name=(EditText)findViewById(R.id.cust_name);
    edittextcust_no=(EditText)findViewById(R.id.cust__number);
    edittextcust_email=(EditText)findViewById(R.id.cust_email);
    edittextcust_address=(EditText)findViewById(R.id.cust_adress);

    save=(Button)findViewById(R.id.Save);

    save.setOnClickListener(new View.OnClickListener() {

        @Override
        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();

            }
        }
    });

}

When I run my app it crashes with this message "android.database.sqlite.SQLiteException: no such table: CUSTINFO_TABLE_NAME (code 1): , while compiling: INSERT INTO CUSTINFO_TABLE_NAME(cust_phone,cust_name,cust_address,cust_email) VALUES (?,?,?,?)"`

Aucun commentaire:

Enregistrer un commentaire