0

我写了一个使用 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”的列。如何解决这个问题呢?

4

3 回答 3

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

或更改为this 有更多的保证

     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

于 2013-08-30T11:35:51.707 回答
0

插入时代码中有拼写错误。

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

于 2013-08-30T11:40:52.473 回答
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();
}

}

于 2013-08-30T12:34:17.337 回答