SQLite 创建 查询 增加

前端之家收集整理的这篇文章主要介绍了SQLite 创建 查询 增加前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

public class UserDBHelper extends sqliteOpenHelper
{
private static String datebase_name = "user_datebase";
public static String table_name = "user_table";
public UserDBHelper(Context context,String name,CursorFactory factory,
int version) {
super(context,name,factory,version);
// TODO Auto-generated constructor stub
}
public UserDBHelper(Context c){
this(c,datebase_name,null,1);
System.out.println("userdb");
}
@Override
public void onCreate(sqliteDatabase db) {
// TODO Auto-generated method stub
System.out.println("enter oncreeat");
db.execsql("create table "+table_name+" (user_id integer primary key autoincrement,user_name text,user_pswd text)");
System.out.println("over oncreate");
}

@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
// TODO Auto-generated method stub
db.execsql("drop table "+table_name);
this.onCreate(db);
}


}

private void insertIP()
{

String ip=IP.getText().toString();
System.out.println("ip:"+ip);
IPDB db = new IPDB(IpActivity.this);
int type=db.getType();
System.out.println("type:"+type);

sqliteDatabase sd= db.getReadableDatabase();
String de="delete from "+IPDB.table_name ;
sd.execsql(de);
System.out.println("ip删除成功!");
sd.close();


sqliteDatabase sd1 = db.getWritableDatabase();
sd1.execsql("insert into "+IPDB.table_name+" values(null,?)",new String[]{ip});
sd1.close();
db.close();
HttpURL.setIpHttp(ip);
Toast.makeText(IpActivity.this,"保存成功!",Toast.LENGTH_SHORT).show();

}

private String getIP() { String ip=null; IPDB db = new IPDB(CybertronMainActivity.this); sqliteDatabase sd = db.getReadableDatabase(); Cursor c = sd.rawQuery("select ip_name from "+IPDB.table_name+"",null); while(c.moveToNext()){ ip = c.getString(0); System.out.println("ip:"+ip); } c.close(); sd.close(); db.close(); return ip; }

猜你在找的Sqlite相关文章