2012-04-29 9 views
1

単純なDbHelperを設定しようとしていて、実行中にエラーが発生しました。"null"に近いSQLiteException:構文エラー:コンパイル中:INSERT OR REPLACE INTO

エラーは次のとおりです。

SQLiteException near "null": syntax error: , while compiling: INSERT OR REPLACE INTO 

私は問題が何であるかを把握することはできませんか?

DbHelperクラスは次のとおりです。

package com.sqlite.test1; 

    import android.content.Context; 
    import android.database.sqlite.SQLiteDatabase; 
    import android.database.sqlite.SQLiteOpenHelper; 

    public class DbHelper extends SQLiteOpenHelper{ 


    public static final int DB_VERSION = 1; 
    public static final String DB_NAME = "time_storage"; 
    public static final String DB_TABLE = "timer_data"; 

    public static final String C_ID = "iderty_id"; 
    public static final String C_DATE = "date"; 
     Context context; 

    public DbHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 
     this.context = context; 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table if not exists " + DB_TABLE + " (" + C_ID + "integer primary key autoincrement, " 
        + C_DATE + " text not null);"); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE); 
     this.onCreate(db);  
    }} 

主なクラスがある:

package com.sqlite.test1; 

import android.app.Activity; 
import android.content.ContentValues; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 

public class SQlite_test1Activity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    DbHelper dbHelper = new DbHelper(SQlite_test1Activity.this); 
    SQLiteDatabase db = dbHelper.getWritableDatabase(); 

    String variable1 = "this is the first text to database"; 
    ContentValues values = new ContentValues(); 
    db.insertWithOnConflict(DbHelper.DB_TABLE, null, values, 
      SQLiteDatabase.CONFLICT_REPLACE); 
    values.put(DbHelper.C_DATE, variable1); 

    db.close(); 
    dbHelper.close(); 

}} 

答えて

2

あなたがそうでなければ、挿入していない、後に、あなたが挿入を行う前に、値を配置する必要はありません何でも

ContentValues values = new ContentValues(); 
values.put(DbHelper.C_DATE, variable1); 
db.insertWithOnConflict(DbHelper.DB_TABLE, null, values, 
     SQLiteDatabase.CONFLICT_REPLACE); 

別の問題が見つかりEDIT

db.execSQL("create table if not exists " + DB_TABLE + " (" + C_ID + "integer primary key autoincrement, " + C_DATE + " text not null);"); 

上記のコードはiderty_idintegerと呼ばれる主キーを作成してこれに

ContentValues values = new ContentValues(); 
db.insertWithOnConflict(DbHelper.DB_TABLE, null, values, 
     SQLiteDatabase.CONFLICT_REPLACE); 
values.put(DbHelper.C_DATE, variable1); 

:これを変更します。

db.execSQL("create table if not exists " + DB_TABLE + " (" + C_ID + " integer primary key autoincrement, " + C_DATE + " text not null);"); 

integer primaryの前に挿入されたスペース...

+0

LogCatが何を言うん:それはこのようにすべきですか? – Barak

+1

ありがとう! – Dori

関連する問題