Arpit Patel
Arpit Patel

Reputation: 1571

Table doesn't created in database

I'm inserting the data into the database but I could be able to insert it due to table is not going to create in my database. I could able to create the database but not able to create table

sqlite returned: error code = 1, msg = no such table: contactTable
Error inserting phone=123-456-789 Id=1 [email protected] key=0r1-2C4E4A3C524A2C523442 name=XYZ ABC

My code is as bellow

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contactbackup.sqlite";
private static final String TABLE_NAME = "contactTable";
private static final String CONTACT_ID = "Id";
private static final String CONTACT_NAME = "name";
private static final String CONTACT_PHONE = "phone";
private static final String CONTACT_EMAIL = "email";
private static final String CONTACT_KEY = "key";

public DBmanagaer(Context con) {
    super(con, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub

}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CONTACT_TABLE_CREATE = "CREATE TABLE "
        + TABLE_NAME.trim() + "(" + CONTACT_ID.trim()
        + " INTEGER PRIMARY KEY , " + CONTACT_NAME.trim() + " TEXT , "
        + CONTACT_PHONE.trim() + " TEXT , " + CONTACT_EMAIL.trim()
        + " TEXT , " + CONTACT_KEY.trim() + " TEXT " + ");";
    if (checkDataBase()) {
    } else {
        try {
            db.execSQL(CONTACT_TABLE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

private boolean checkDataBase() {
    // TODO Auto-generated method stub
    File dbFile = new File(DB_PATH + DATABASE_NAME);
    return dbFile.exists();
}

public void adddata(contactDto con) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(CONTACT_ID, Integer.parseInt(con.ID));
    values.put(CONTACT_NAME, con.name);
    values.put(CONTACT_PHONE, con.contactphone);
    values.put(CONTACT_EMAIL, con.email);
    values.put(CONTACT_KEY, con.key);
    db.insert(TABLE_NAME, null, values);
    db.close(); // Closing database connection
}

public ArrayList<contactDto> getAlldata() {
    String selectQuery = "SELECT  * FROM " + TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            contactDto con = new contactDto();
            con.ID = cursor.getString(0);
            con.name = cursor.getString(1);
            con.contactphone = cursor.getString(2);
            con.email = cursor.getString(3);
            con.key = cursor.getString(4);
            conList.add(con);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    // return contact list
    return conList;
}

I am getting the error in the following line:

db.insert(TABLE_NAME, null, values);

I have gone through some of the tutorials, those all are the same. I don't know where i went wrong.

Upvotes: 0

Views: 267

Answers (1)

Ali Imran
Ali Imran

Reputation: 9237

Use this string to create your table, In this string i just removed the semicolon at the end of the query.

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CONTACT_TABLE_CREATE = "CREATE TABLE "
        + TABLE_NAME.trim() + "(" + CONTACT_ID.trim()
        + " INTEGER PRIMARY KEY , " + CONTACT_NAME.trim() + " TEXT , "
        + CONTACT_PHONE.trim() + " TEXT , " + CONTACT_EMAIL.trim()
        + " TEXT , " + CONTACT_KEY.trim() + " TEXT " + ")";


        try {
            db.execSQL(CONTACT_TABLE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }

}

Upvotes: 3

Related Questions