代码是活动
private void parseData(JSONArray array) {
for (int i = 0; i < array.length(); i++) {
VideoJson model = new VideoJson();
JSONObject json = null;
try {
json = array.getJSONObject(i);
String vedio_name = json.getString(Config.TAG_VIDEO_TEXT);
String vedio_url = json.getString(Config.TAG_VIDEO_URL);
String vedio_img = json.getString(Config.TAG_VIDEO_IMAGE);
model.setName_video(vedio_name);
model.setImageUrl_video(vedio_url);
model.setVedio_thambnail(vedio_img);
Log.e("name","is =>"+vedio_name);
Log.e("url","is =>"+vedio_url);
Log.e("image","is =>"+vedio_img);
db.addVideos(model);
Log.e("database","value is"+db);
} catch (JSONException e) {
e.printStackTrace();
}
并在数据库中插入值
public class VideoDataBase extends SQLiteOpenHelper {
private static final int DB_VERSION = 4;
private static final String DB_NAME = "appname";
private static final String TABLE_VIDEOS= "videos";
private static final String KEY_VIDEO_ID = "video_id";
private static final String KEY_VIDEO_NAME = "video_id_text";
private static final String KEY_VIDEO_URL ="video_id_url";
private static final String KEY_VIDEO_THUMBNAIL ="video_thumb";
private static final String CREATE_TABLE_VIDEO = "CREATE TABLE "
+ TABLE_VIDEOS
+ " ( "
+ KEY_VIDEO_ID + " INTEGER PRIMARY KEY, "
+ KEY_VIDEO_NAME + " TEXT, "
+ KEY_VIDEO_URL + " TEXT, "
+ KEY_VIDEO_THUMBNAIL + " TEXT) ";
public VideoDataBase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_VIDEO);
Log.e("table name","==>"+CREATE_TABLE_VIDEO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TextDataBase.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS "+TABLE_VIDEOS);
onCreate(db);
}
public void addVideos(VideoJson model){
SQLiteDatabase db = this.getWritableDatabase();
try{
ContentValues values = new ContentValues();
values.put(KEY_VIDEO_NAME, model.getName_video());
values.put(KEY_VIDEO_URL, model.getImageUrl_video());
values.put(KEY_VIDEO_THUMBNAIL, model.getVedio_thambnail());
db.insert(TABLE_VIDEOS, null, values);
Log.e("this db","value is"+values);
db.close();
}catch (Exception e){
Log.e("problem",e+"");
}
}
public List<VideoJson> getAllVideos() {
SQLiteDatabase db = this.getReadableDatabase();
List<VideoJson> videoList = null;
try{
videoList = new ArrayList<VideoJson>();
String QUERY = "SELECT * FROM "+TABLE_VIDEOS;
Cursor cursor = db.rawQuery(QUERY, null);
if(!cursor.isLast())
{
while (cursor.moveToNext())
{
VideoJson videolist = new VideoJson();
videolist.setVideo_id(cursor.getInt(0));
videolist.setName_video(cursor.getString(1));
videolist.setImageUrl_video(cursor.getString(2));
videolist.setVedio_thambnail(cursor.getString(3));
videoList.add(videolist);
}
}
db.close();
}catch (Exception e){
Log.e("error",e+"");
}
return videoList;
}
数据库
input.html
答案 0 :(得分:0)
尝试在插入前删除db表。
private void parseData(JSONArray array) {
db.deleteTABLE_VIDEOS();
for (int i = 0; i < array.length(); i++) {
VideoJson model = new VideoJson();
JSONObject json = null;
try {
json = array.getJSONObject(i);
String vedio_name = json.getString(Config.TAG_VIDEO_TEXT);
String vedio_url = json.getString(Config.TAG_VIDEO_URL);
String vedio_img = json.getString(Config.TAG_VIDEO_IMAGE);
model.setName_video(vedio_name);
model.setImageUrl_video(vedio_url);
model.setVedio_thambnail(vedio_img);
Log.e("name","is =>"+vedio_name);
Log.e("url","is =>"+vedio_url);
Log.e("image","is =>"+vedio_img);
db.addVideos(model);
Log.e("database","value is"+db);
} catch (JSONException e) {
e.printStackTrace();
}