0

I am getting error while inserting data in to sqlite data base.This is my query. What is the problem.

I am not able get out of this. I think error because of space in creating table, where i need to make changes.

@Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PH_NO + " TEXT," + KEY_MACID + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

This is the error i am getting.

Error inserting macid=benbena phone_number=01-04-2016 17:32:31 PM name=testlock1@acttv.in android.database.sqlite.SQLiteException: table contacts has no column named macid (code 1): , while compiling: INSERT INTO contacts(macid,phone_number,name) VALUES (?,?,?)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
benarjee bojja
  • 348
  • 1
  • 3
  • 15

1 Answers1

0

It is advised to create a Contact Class to handle the data easily first. However, without that, here is how I would accomplish the database entry task:

Custom SQLiteOpenHelper Class:

public class DBHandlerTest extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "database.db";

    public static final int DATABASE_VERSION = 1;

    public static final String TABLE_CONTACTS = "contacts";

    public static final String KEY_ID = "id";
    public static final String KEY_NAME = "name";
    public static final String KEY_PH_NO = "phone_number";
    public static final String KEY_MACID = "macid";


    public static final String CREATE_CONTACTS_TABLE =
            "CREATE TABLE " + TABLE_CONTACTS + "("
                    + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + KEY_NAME + " TEXT(255) NOT NULL, "
                    + KEY_PH_NO + " TEXT(255) NOT NULL, "
                    + KEY_MACID + " TEXT(255) NOT NULL" + ")";


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


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
        //re-create db
        onCreate(db);
    }

    public void insertContact(String name, String phoneNumber, String macID) {

        //create db instance
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contactValues = new ContentValues();

        //get the values
        contactValues.put(KEY_NAME, name);
        contactValues.put(KEY_PH_NO, phoneNumber);
        contactValues.put(KEY_MACID, macID);

        //insert row into db
        db.insert(TABLE_CONTACTS, null, contactValues);
        db.close();

    }

}

Implementing the method:

DBHandlerTest db = new DBHandlerTest(this);
db.insertContact("test@test.com", "0800-123456", "test_id"); 
db.close();
blueprintchris
  • 1,053
  • 1
  • 16
  • 38