Android SQLiteDatabase插入错误,可能出现创建问题

时间:2014-02-22 21:35:57

标签: android sqlite

每次尝试调用addSatInstance()时,我都会在logcat中收到错误。我从SQL lite得到插入错误。我不知道出了什么问题。我认为在创建db时或在用于创建表的字符串中出现问题。

这是我的错误:

02-22 16:55:24.821: E/SQLiteDatabase(1073): Error inserting id=0 altitude=404.0 name= instance  userid=0 bearing=404.0 provider=Value made it lng=404.0 accuracy=404.0 lat=404.0
02-22 16:55:24.821: E/SQLiteDatabase(1073): android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at sat.save.SCSQLHelper.addSatInstance(SCSQLHelper.java:89)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at sat.save.MainActivity$1.onClick(MainActivity.java:105)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.view.View.performClick(View.java:4240)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.view.View$PerformClick.run(View.java:17721)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.os.Handler.handleCallback(Handler.java:730)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.os.Looper.loop(Looper.java:137)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at java.lang.reflect.Method.invoke(Method.java:525)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-22 16:55:24.821: E/SQLiteDatabase(1073):     at dalvik.system.NativeStart.main(Native Method)

这是我的SQLHelper

public class SCSQLHelper extends SQLiteOpenHelper {


// Sat table name
private static final String TABLE_SAT = "sat";

// Sat Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_USERID = "userid";
private static final String KEY_LAT = "lat";
private static final String KEY_LNG = "lng";
private static final String KEY_PROVIDER = "provider";
private static final String KEY_ACCURACY = "accuracy";
private static final String KEY_ALTITUDE = "altitude";
private static final String KEY_BEARING = "bearing";

private static final String[] COLUMNS = {KEY_ID,KEY_NAME,KEY_USERID,KEY_LAT,KEY_LNG,KEY_PROVIDER,KEY_ACCURACY,KEY_ALTITUDE,KEY_BEARING};

public SCSQLHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);  
}

@Override
public void onCreate(SQLiteDatabase db) {
    // SQL statement to create book table
    String CREATE_SAT_TABLE = "CREATE TABLE sat ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
            "name TEXT, "+
            "userid INTEGER, "+
            "lat REAL, "+
            "lng REAL, "+
            "provider TEXT, "+
            "accuracy REAL, "+
            "altitude REAL, "+
            "bearing REAL )";

    // create books table
    db.execSQL(CREATE_SAT_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older books table if existed
    db.execSQL("DROP TABLE IF EXISTS sat");

    // create fresh books table
    this.onCreate(db);
}

public void addSatInstance(SatInstance satinstance){
    //for logging
    Log.d("addBook", satinstance.toString()); 

    // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(KEY_ID, satinstance.getId()); 
    values.put(KEY_NAME, satinstance.getName());
    values.put(KEY_USERID, satinstance.getUserId());
    values.put(KEY_LAT, satinstance.getLat());
    values.put(KEY_LNG, satinstance.getLng());
    values.put(KEY_PROVIDER, satinstance.getProvider());
    values.put(KEY_ACCURACY, satinstance.getAccuracy());
    values.put(KEY_ALTITUDE, satinstance.getAltitude());
    values.put(KEY_BEARING, satinstance.getBearing());

    // 3. insert
    db.insert(TABLE_SAT, // table
            null, //nullColumnHack
            values); // key/value -> keys = column names/ values = column values

    // 4. close
    db.close(); 
}   

}

这是我在表中使用的类

public class SatInstance {

private int id;
private String name = " instance ";
private int userid;
private double lat = 0;
private double lng = 0;
private String provider = new String("Data Error");
private float accuracy = 0;
private double altitude = 0;
private float bearing = 0;

public SatInstance(){}

public SatInstance(String name, int userid, double lat, double lng, String provider, float accuracy, double altitude, float bearing) {
    super();
    this.name = name;
    this.userid = userid;
    this.lat = lat;
    this.lng = lng;
    this.provider = provider;
    this.accuracy = accuracy;
    this.altitude = altitude;
    this.bearing = bearing;
}

这就是所谓的地方:

SatInstance satinstance = new SatInstance();


            satinstance.setLat(404);
            satinstance.setLng(404);
            satinstance.setProvider("Value made it");
            satinstance.setAccuracy(404);
            satinstance.setAltitude(404);
            satinstance.setBearing(404);
            db.addSatInstance(satinstance);

如果您需要更多代码,请与我们联系。谢谢!

2 个答案:

答案 0 :(得分:3)

删除

values.put(KEY_ID, satinstance.getId()); 

因此数据库可以为您的行生成autoincrement个ID。

如果您从未初始化satinstance中的id,那么它将保持其默认值0并显式插入具有相同主键值的多行会导致SQLiteConstraintException: PRIMARY KEY must be unique

答案 1 :(得分:0)

您已插入ID = 0的项目,并且不允许重复项目。由于键被定义为自动增量,因此您可以在插入操作中省略它。

如果您在插入内容中提供id,则会覆盖autoincrement行为。