emreturka
emreturka

Reputation: 876

Table has no column named error while INSERTING

I have written an app that using sqlite.I can't see any problem but it is giving this error.Here is my codes.

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


@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "";

    /*
     *  db.execSQL("create table mytable ("
      + "id integer primary key autoincrement," 
      + "glucose text,"
      + "time text," // added a ','
      + "date text" + ");");
     * */

    db.execSQL("create table kisiler ("
            +"id integer primary key autoincrement, "
            +"adsoyad text, "
            +"telefon text, "
            +"meslegi text, "
            +"uzmanlik1 text, "
            +"uzmanlik2 text, "
            +"tecrubeyili integer" + ");");
    //Tablomuzu burada oluşturduk

}


@Override
public void onUpgrade(SQLiteDatabase db, int eskiSurum, int yeniSurum) {

    /*
     * Veritabanı sürümlerini denetliyoruz.Eğer yeni sürüm gelirse eski bilgiler 
     * silinecektir.Bura da kontroller yapılıyor
     */

    if(eskiSurum >= yeniSurum)
        return ;
    String sql = null;
    if(eskiSurum == 1)
        sql = "alter table "+TABLO_ADI+" add note text;";
    if(eskiSurum == 2)
        sql = "";
    Log.d("Veri güncelleme durumları  ","Not edilen : "+sql);
    if(sql!=null)
        db.execSQL(sql);
}

And I made insert in this codes.

 ekleButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            cv.put(SQLiteVeritabaniBaglantisi.ADISOYADI, adsoyad.getText().toString());
            cv.put(SQLiteVeritabaniBaglantisi.TELEFON, telefon.getText().toString());
            cv.put(SQLiteVeritabaniBaglantisi.MESLEGI, meslek.getSelectedItem().toString());
            cv.put(SQLiteVeritabaniBaglantisi.UZMANLIGI1, uzm1.getText().toString());
            cv.put(SQLiteVeritabaniBaglantisi.UZMANLIGI2, uzm2.getText().toString());
            cv.put(SQLiteVeritabaniBaglantisi.TECRUBEYILI,tecrubeyili.getSelectedItem().toString());


            try {
                db.insert(SQLiteVeritabaniBaglantisi.TABLO_ADI, null, cv);
                /*
                 * Veritabanımıza gerekli nesnelerimizi paramterelei
                 * ile gönderidk.Bu nesneler gerekli kolonlara yerleştirilecektir.
                 * */

                Toast.makeText(getApplicationContext(), "Ekleme işlemi yapıldı", 
                        Toast.LENGTH_SHORT).show();

            } catch (Exception e) {
                e.printStackTrace();
            }
            temizle();
            spKaydedilenAdapter.clear();
            doldur();
            kayitliKisiSayisi();
        }
    });

Where is my error? The logcat's message is;

08-30 11:26:38.841: E/SQLiteDatabase(731): android.database.sqlite.SQLiteException: table kisiler has no column named uzmanlik1: , while compiling: INSERT INTO kisiler(uzmanlik1,uzmanlik2,adsoyad,telefon,tecrubeyili,meslegi) VALUES (?,?,?,?,?,?)

But there is a column that named "uzmanlik1". How to solve this problem?

Upvotes: 0

Views: 1607

Answers (3)

Shivang Trivedi
Shivang Trivedi

Reputation: 2182

use this link : this complete example with source code. http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contactsManager";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";

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

// Creating Tables
@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" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new contact
void addContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName()); // Contact Name
    values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
}

// Getting single contact
Contact getContact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
            KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2));
    // return contact
    return contact;
}

// Getting All Contacts
public List<Contact> getAllContacts() {
    List<Contact> contactList = new ArrayList<Contact>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Contact contact = new Contact();
            contact.setID(Integer.parseInt(cursor.getString(0)));
            contact.setName(cursor.getString(1));
            contact.setPhoneNumber(cursor.getString(2));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

// Updating single contact
public int updateContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName());
    values.put(KEY_PH_NO, contact.getPhoneNumber());

    // updating row
    return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });
}

// Deleting single contact
public void deleteContact(Contact contact) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
            new String[] { String.valueOf(contact.getID()) });
    db.close();
}


// Getting contacts Count
public int getContactsCount() {
    String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

}

Upvotes: 0

Tarsem Singh
Tarsem Singh

Reputation: 14199

if you solved the current problem after that you will get one more Exception that is NumberFormatException

as your column tecrubeyili is of integer type and you are trying to store string inside it

1. You are creating column name uzmanlik1 here

 db.execSQL("create table kisiler ("
            +"id integer primary key autoincrement, "
            +"adsoyad text, "
            +"telefon text, "
            +"meslegi text, "
            +"uzmanlik1 text, "
            +"uzmanlik2 text, "
            +"tecrubeyili integer" + ");");

but when you are trying to insert values you are using SQLiteVeritabaniBaglantisi.UZMANLIGI1

so make sure this variable has value uzmanlik1

or Change to This has More Surety

     cv.put("adsoyad", adsoyad.getText().toString());
     cv.put("telefon", telefon.getText().toString());
     cv.put("meslegi", meslek.getSelectedItem().toString());
     cv.put("uzmanlik1", uzm1.getText().toString());
     cv.put("uzmanlik2", uzm2.getText().toString());

     // Note code of following Line it is Parsed to integer
     cv.put("tecrubeyili",Integer.parseInt(tecrubeyili.getSelectedItem().toString()));

2. if after following Above step still getting the problem then Completely uninstall the application before Re-Installing it !

Upvotes: 3

user1764879
user1764879

Reputation: 122

There is spell mistake in the code when inserting.

you are using UZMANLIGI1 in the insert code instead of uzmanlik1 ..

Upvotes: 0

Related Questions