-1

this is my code :

public void onCreate(SQLiteDatabase db) 
{
    String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
            + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," // and auto increment will be handled with                            primary key
            + KEY_UID + " TEXT,"
            + KEY_NAME + " TEXT," 
            + KEY_PH_NO + " TEXT,"
            + KEY_Comapny + " TEXT,"
            + KEY_email + " TEXT," 
            + KEY_country + " TEXT," 
            + KEY_street + " TEXT," 
            + KEY_city + " TEXT,"+ KEY_state + " TEXT," 
            + KEY_zip + " TEXT);";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

I am getting below exception :

: E/SQLiteDatabase(2645): android.database.sqlite.SQLiteException: table contacts has no column named uid (code 1): , while compiling: INSERT INTO contacts(uid,zip,phone_number,email,company_name,street,name,state,city,country) VALUES (?,?,?,?,?,?,?,?,?,?)

Please help. Thanks

Nibha Jain
  • 7,742
  • 11
  • 47
  • 71

3 Answers3

2

There's a syntax error in your CREATE TABLE so obviously it has not been run. You need a space between EXISTS and the table name.

Since onCreate() with your current SQL has not been run, you have an older version of your database file around. Uninstall your app to remove it and make onCreate() run again. See When is SQLiteOpenHelper onCreate() / onUpgrade() run? for more.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
1

Replace this query:

String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
                + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," // and auto increment will be handled with                            primary key
                + KEY_UID + " TEXT,"
                + KEY_NAME + " TEXT," 
                + KEY_PH_NO + " TEXT,"
                + KEY_Comapny + " TEXT,"
                + KEY_email + " TEXT," 
                + KEY_country + " TEXT," 
                + KEY_street + " TEXT," 
                + KEY_city + " TEXT,"+ KEY_state + " TEXT," 
                + KEY_zip + " TEXT);";
        db.execSQL(CREATE_CONTACTS_TABLE);

with this:

String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " 
                + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," // and auto increment will be handled with                            primary key
                + KEY_UID + " TEXT,"
                + KEY_NAME + " TEXT," 
                + KEY_PH_NO + " TEXT,"
                + KEY_Comapny + " TEXT,"
                + KEY_email + " TEXT," 
                + KEY_country + " TEXT," 
                + KEY_street + " TEXT," 
                + KEY_city + " TEXT,"+ KEY_state + " TEXT," 
                + KEY_zip + " TEXT);";
        db.execSQL(CREATE_CONTACTS_TABLE);

Note: There is a syntax error in your CREATE TABLE query, You need a space between EXISTS and the TABLE_CONTACTS.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0

Here is Solution : i facing same issue and now i fix it

we you want to make database app then you have two java classes first for Parameters Params.java , and other is Contact.java where we make constructors, getter&setter etc.

Param.java Class Code Below: (see Error in Line:5 and 6) and fix that reference

package com.andro.background.crud_db.param;

public class Params {
    public static final int DB_VERSION=1;
    public static final String DB_NAME="contacts_db"; //(-_-)check this, My mistake was "contact_db" , i write wrong name so it shows error
    public static final String TABLE_NAME="contacts_table"; // Right is "contact_table", this kinda error in your code

    //key of our table in db
    public static final String KEY_ID="id";
    public static final String KEY_NAME="name";
    public static final String KEY_PHONE="phone_number";
}

(check this error in upper code) if you do same mistake then your error will be like this below code

2020-02-03 02:57:24.432 10705-10705/com.andro.background.cruddb E/SQLiteLog: (1) table contacts_table has no column named phone_number
2020-02-03 02:57:24.434 10705-10705/com.andro.background.cruddb E/SQLiteDatabase: Error inserting name=sunnykanumber phone_number=9090909090
    android.database.sqlite.SQLiteException: table contacts_table has no column named phone_number (code 1): , while compiling: INSERT INTO contacts_table(name,phone_number) VALUES (?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
        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:1546)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1417)
        at com.andro.background.cruddb.data.MyDbHandler.addContact(MyDbHandler.java:62)
        at com.andro.background.cruddb.MainActivity.onCreate(MainActivity.java:31)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)


AG-Developer
  • 361
  • 2
  • 10