没有这样的表:ogrenci :,编译时:INSERT INTO

时间:2013-01-03 05:01:03

标签: android sqlite sqliteopenhelper

我正在尝试配置一个简单的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();  
                            }       });

    } }

2 个答案:

答案 0 :(得分: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列之间添加“,”

答案 1 :(得分: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();
}