1

我正在尝试配置一个简单的SQLiteOpenHelper,并在执行时出错。

错误是:

ogrenci没有名为的列field__final

编译时:

INSERT INTO ogrenci(field__final, field__numarasi, field_ad, field__vize, field__soyad) VALUES(?, ?, ?, ?, ?);

我无法弄清楚是什么问题?

dbSQLite.java班级 :

package com.example.sqllitevt;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class dbSQLite extends SQLiteOpenHelper {

    private static final String VERITABANI = "ogrenciler";
    private static final String TABLO_ADI = "ogrenci";
    private static final String KISI_ADI = "field_ad";
    private static final String KISI_SOYADI = "field__soyad";
    private static final String KISI_NUMARASI = "field__numarasi";
    private static final String KISI_VIZE = "field__vize";
    private static final String KISI_FINAL = "field__final";
    private static int SURUM = 1;

    public dbSQLite(Context context) {
        super(context, VERITABANI, null, SURUM);
        // TODO Auto-generated constructor stub
    }

    @Override 

    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

          String sql = "CREATE TABLE " + TABLO_ADI + " ("
                                  + "id INTEGER PRIMARY KEY AUTOINCREMENT," 
                                  + KISI_ADI + " TEXT NOT NULL,"
                                  + KISI_SOYADI + " TEXT NOT NULL," 
                                  + KISI_NUMARASI + " TEXT" 
                                  + KISI_VIZE + "TEXT" 
                                  + KISI_FINAL + "TEXT" + ");";

        db.execSQL(sql);


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion >= newVersion)
            return;

        String sql = null;
        if (oldVersion == 1)
            sql = "alter table " + TABLO_ADI + " add note text;";
        if (oldVersion == 2)
            sql = "";

        Log.d("EventsData", "onUpgrade  : " + sql);
        if (sql != null)
            db.execSQL(sql);

    }


    public void kaydet(String k_ad, String k_soyad, String k_numara,
            String k_vize, String k_finali) {
        try{
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv=new ContentValues();

        cv.put(this.KISI_ADI, k_ad);
        cv.put(this.KISI_SOYADI, k_soyad);
        cv.put(this.KISI_NUMARASI, k_numara);
        cv.put(this.KISI_VIZE, k_vize);
        cv.put(this.KISI_FINAL, k_finali);

        db.insertOrThrow(this.TABLO_ADI,null,cv);

        Log.d("DB->Basarili","Basarılı");
        db.close();
        }catch(SQLiteException ex){
            Log.e("DB->Kayit", "Basarisiz oldu:"+ex.getMessage() );
        }

    }

}

ekle.java班级:

package com.example.sqllitevt;

import android.os.Bundle; import android.app.Activity; import android.text.Editable; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TabHost; import android.widget.TabHost.TabSpec; import android.widget.Toast;

public class ekle extends Activity {    dbSQLite db;    Button kaydet;  EditText ad,soyad,numara,vize,finali;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sekme_ekle);

        kaydet=(Button) findViewById(R.id.btnKaydet);
        ad=(EditText)findViewById(R.id.txtAd);
        soyad=(EditText)findViewById(R.id.txtSoyad);
        numara=(EditText)findViewById(R.id.txtNumara);
        vize=(EditText)findViewById(R.id.txtVize);
        finali=(EditText)findViewById(R.id.txtFinal);

        kaydet.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                db=new dbSQLite(getApplicationContext());

                String k_ad=ad.getText().toString();
                String k_soyad=soyad.getText().toString();
                String k_numara=numara.getText().toString();
                String k_vize= vize.getText().toString();
                String k_finali=finali.getText().toString();


                db.kaydet(k_ad, k_soyad, k_numara ,k_vize,k_finali);

                    Toast.makeText(getApplicationContext(), "Kayıt Edildi", Toast.LENGTH_LONG).show();  
                            }       });

    } }
4

2 回答 2

1

将查询更改CREATE TABLE为:

     String sql = "CREATE TABLE " + TABLO_ADI + " ("
                              + "id INTEGER PRIMARY KEY AUTOINCREMENT, " 
                              + KISI_ADI + " TEXT NOT NULL, "
                              + KISI_SOYADI + " TEXT NOT NULL, " 
                              + KISI_NUMARASI + " TEXT, " 
                              + KISI_VIZE + "TEXT, " 
                              + KISI_FINAL + "TEXT );";

目前您没有在和列 之间添加“、KISI_NUMARASIKISI_VIZEKISI_FINAL

于 2013-01-03T05:05:24.590 回答
0

检查您的表是否有一个名为field_final. 我建议你重新创建表。该错误表示该列field_final存在

考虑为您的列使用更简单的名称,例如,使用类似finalNotufinal_notu代替field__final. 使用复杂的列名通常是个主意。

我认为这个方法会给你这个想法......这个方法应该属于你的dbSQLHelper类。

public void reCreate() {
     SQLiteDatabase db = this.getWritableDatabase();
     db.execSQL("DROP TABLE IF EXISTS " + TABLO_ADI);
     String sql = "CREATE TABLE " + TABLO_ADI + " ("
                          + "id INTEGER PRIMARY KEY AUTOINCREMENT," 
                          + KISI_ADI + " TEXT NOT NULL, "
                          + KISI_SOYADI + " TEXT NOT NULL, " 
                          + KISI_NUMARASI + " TEXT, " 
                          + KISI_VIZE + "TEXT, " 
                          + KISI_FINAL + "TEXT" + ");"; 
     db.execSQL(sql);
     db.close();
}
于 2013-01-03T18:35:04.163 回答