添加数据方法
SqliteHepler hepler = new SqliteHepler(Details_Activity.this);
SQLiteDatabase db = hepler.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("id", list_d.id);
values.put("ima", list_d.pic.get(0));
values.put("mar_price", list_d.limitPrice);
values.put("shop_price", list_d.price);
values.put("title", list_d.name);
db.insert(SqliteHepler.TABLE_NAME_USER, null, values);
Toast.makeText(Details_Activity.this, "已添加收藏", Toast.LENGTH_SHORT).show();
de_collect.setBackgroundResource(R.drawable.head_button_selected);
查询数据方法
list_c = new ArrayList<Car_Vo>();
Cursor c = db.query(false, SqliteHepler.Table_Name_User, null, null, null, null, null, null, null, null);
while(c.moveToNext()){
String sid = c.getString(c.getColumnIndex("id"));
String sima = c.getString(c.getColumnIndex("ima"));
String sshop_price = c.getString(c.getColumnIndex("shop_price"));
String stitle = c.getString(c.getColumnIndex("title"));
car_vo = new Car_Vo();
car_vo.setId(sid);
car_vo.setIma(sima);
car_vo.setPrice(sshop_price);
car_vo.setName(stitle);
list_c.add(car_vo);
Log.i("shop", car_vo.toString());
建表语句
package com.example.administrator.redbaby_9s.view;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SqliteHepler extends SQLiteOpenHelper {
public static final String Table_Name_User = "shopuser";
public static final String Table_Name_Login = "loginuser";
public static final String SQLITE_DATABASE_NAME = "myDB.db";
public static final int SQLITE_DATABASE_VERSION = 1;
public static final String TABLE_NAME_USER = "user";
public SqliteHepler(Context context) {
super(context, SQLITE_DATABASE_NAME, null, SQLITE_DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
String sqlCreateStr = "CREATE TABLE " + TABLE_NAME_USER + "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+"id varchar(100),"
+ "ima varchar(100),"
+ "mar_price varchar(100),"
+"shop_price varchar(100),"
+ "title varchar(100)" + ")";
db.execSQL(sqlCreateStr);
String sql_shop = "CREATE TABLE " + Table_Name_User + "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+"id varchar(100),"
+ "ima varchar(100),"
+"shop_price varchar(100),"
+ "title varchar(100)"
+ ")";
db.execSQL(sql_shop);
String sql_login = "CREATE TABLE " + Table_Name_Login + "("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT,"
+"name varchar(100),"
+ "pass varchar(100)"
+ ")";
db.execSQL(sql_login);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}