android_SQLite简单存储基本用法




//创建表  
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;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值