INSERTING时,表没有名为error的列

时间:2013-08-30 11:33:52

标签: android sqlite

我已经编写了一个使用sqlite的应用程序。我看不到任何问题,但是它给出了这个错误。这是我的代码。

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

我在这段代码中插入了。

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

我的错误在哪里? logcat的消息是;

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 (?,?,?,?,?,?)

但是有一个名为“uzmanlik1”的专栏。如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

如果您之后解决了当前问题,您将再获得一个NumberFormatException

的异常

因为您的列tecrubeyiliof integer type您正试图在其中存储字符串

1。您正在此处创建列名uzmanlik1

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

但是当您尝试插入使用SQLiteVeritabaniBaglantisi.UZMANLIGI1

的值时

因此请确保此变量的值为uzmanlik1

或更改为这有更多的保证

     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。如果按照上述步骤仍然遇到问题,那么Completely uninstall应用程序before Re-Installing it

答案 1 :(得分:0)

插入时代码中存在拼写错误。

您在插入代码中使用UZMANLIGI1而不是uzmanlik1 ..

答案 2 :(得分:0)

使用此链接:这个包含源代码的完整示例。 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();
}

}