//创建表
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBHelper extends SQLiteOpenHelper{
//dbname的用意是在目录下的文件名
private static String dbname = "asso.db";
private static int version = 1;
public DBHelper(Context context){
super(context,dbname,null,version);
}
public DBHelper(Context context,String dbname,CursorFactory factory,int version){
super(context,dbname,factory,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表的代码
db.execSQL("create table if not exists product(productCode varchar(20) primary key,productname varchar(50),wholeSalePrice float,relailPrice float)");
db.execSQL("create table if not exists customer(cno varchar(15) primary key,customername varchar(40),addr varchar(100)," +
"ceo varchar(12),tel varchar(20))");
db.execSQL("create table if not exists loginedInfo(mobile varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
//数据库升级调用
//drop的含义是删除真个表 而delete只能删除表中的数据 而表存在
db.execSQL("drop table if not exists product");
db.execSQL("drop table if not exists customer");
db.execSQL("drop table if not exists loginedInfo");
//重新建立
onCreate(db);
}
}
//使用
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.ambow.util.DBHelper;
public class UserService {
private DBHelper dbHelper;
public UserService(Context context){
dbHelper = new DBHelper(context);
}
public void insertMobile(String mobile){
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from loginedInfo where mobile=?",
new String[]{mobile});
while(cursor.moveToNext()){
db.close();
return;
}
db.beginTransaction();
db.execSQL("insert into loginedInfo values(?)",new Object[]{mobile});
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}
public String getCurrentLoginedMobile(){
String mobile = null;
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from loginedInfo",new String[]{});
while(cursor.moveToNext()){
mobile = cursor.getString(0);
}
cursor.close();
db.close();
return mobile;
}
}
//创建表
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DBHelper extends SQLiteOpenHelper{
//dbname的用意是在目录下的文件名
private static String dbname = "asso.db";
private static int version = 1;
public DBHelper(Context context){
super(context,dbname,null,version);
}
public DBHelper(Context context,String dbname,CursorFactory factory,int version){
super(context,dbname,factory,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表的代码
db.execSQL("create table if not exists product(productCode varchar(20) primary key,productname varchar(50),wholeSalePrice float,relailPrice float)");
db.execSQL("create table if not exists customer(cno varchar(15) primary key,customername varchar(40),addr varchar(100)," +
"ceo varchar(12),tel varchar(20))");
db.execSQL("create table if not exists loginedInfo(mobile varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
//数据库升级调用
//drop的含义是删除真个表 而delete只能删除表中的数据 而表存在
db.execSQL("drop table if not exists product");
db.execSQL("drop table if not exists customer");
db.execSQL("drop table if not exists loginedInfo");
//重新建立
onCreate(db);
}
}
//使用
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.ambow.util.DBHelper;
public class UserService {
private DBHelper dbHelper;
public UserService(Context context){
dbHelper = new DBHelper(context);
}
public void insertMobile(String mobile){
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from loginedInfo where mobile=?",
new String[]{mobile});
while(cursor.moveToNext()){
db.close();
return;
}
db.beginTransaction();
db.execSQL("insert into loginedInfo values(?)",new Object[]{mobile});
db.setTransactionSuccessful();
db.endTransaction();
db.close();
}
public String getCurrentLoginedMobile(){
String mobile = null;
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from loginedInfo",new String[]{});
while(cursor.moveToNext()){
mobile = cursor.getString(0);
}
cursor.close();
db.close();
return mobile;
}
}