我尝试从JSON中提取数据并将其放入数据库中。
这是我插入的地方:
for (int i = 0; i < listArray.length(); i++) {
//extraction to get "time" and "weather_description"
//............
//insertion:
ContentValues values = new ContentValues();
//values.put(WeatherEntry._ID,i);
values.put(WeatherEntry.DATE_COLUMN, time);
values.put(WeatherEntry.DESCRIPTION_COLUMN, weather_description);
getContentResolver().insert(WeatherEntry.CONTENT_URI, values);
}
SQLiteOpenHelper:
public class WeatherDb extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
static final String DATABASE_NAME = "weather.db";
public WeatherDb(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String WEATHER_QUERY = "CREATE TABLE "+ WeatherEntry.WEATHER_TABLE_NAME+" ("+
WeatherEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
WeatherEntry.DATE_COLUMN+" TEXT, "+
WeatherEntry.DESCRIPTION_COLUMN+" TEXT );";
sqLiteDatabase.execSQL(WEATHER_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
只有第一行(
_ID=0
)才能进入数据库。
我也收到了错误消息:
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: weather._id (code 1555)
答案 0 :(得分:-1)
尝试添加AUTOINCREMENT
这样的内容,而不是插入i
:
WeatherEntry._ID+" INTEGER PRIMARY KEY AUTOINCREMENT"