这篇文章主要介绍“Android中的SQLite有什么用”,在日常操作中,相信很多人在Android中的SQLite有什么用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Android中的SQLite有什么用”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
在Android系统中内置了一个数据库,那就是SQLite。SQlite是一个轻量级,嵌入式的关系数据库
它的运算速度非常快,占用资源很少,通常只需要几百KB的内存,因此特别适合在移动设备上使用,SQLite不仅支持标准的SQL语法还遵循了数据库的ACID事务,它相比于一般的数据库快很多,甚至不需要设置用户和密码就能使用。正是因为Android把这个功能及其强大的数据库内嵌到系统中,才使得本地持久化有了一次质的飞越
Android提供了一个抽象类SQLiteOpenHelper,继承该类,并且实现onCreate和onUpgrade就能创建数据库
onCreate是创建数据库时调用,onUpgrade是升级数据库时调用
首先创建一个继承SQLiteOpenHelper的类
public class MySQLiteHelper extends SQLiteOpenHelper {
private static String CREATE_TABLE_USER="create table users("+
"id integer primary key autoincrement,"+
"userid text,password text)";
private Context sContext;
public MySQLiteHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
sContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_USER);
Toast.makeText(sContext,"成功创建数据表",Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
在MainActivity中
public class MainActivity extends AppCompatActivity {
private MySQLiteHelper sqLiteHelper;
private SQLiteDatabase myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btCreateDb=(Button)findViewById(R.id.btCreateDb);
btCreateDb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sqLiteHelper=new MySQLiteHelper(MainActivity.this,"usersdb.db",null,1);
myDb=sqLiteHelper.getWritableDatabase();
}
});
即可完成创建
而所谓的升级数据库,其实就是SQLiteOpenHelper的版本号如果比当前打,就需要onUpgrade升级,如果比当前小就需要onDowngrade
public class MySQLiteHelper extends SQLiteOpenHelper {
private static String CREATE_TABLE_USER="create table users("+
"id integer primary key autoincrement,"+
"userid text,password text)";
private static String CREATE_TABLE_TYPE="create table types("+
"id integer primary key autoincrement,"+
"type_code,describe text)";
private Context sContext;
public MySQLiteHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
sContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_USER);
db.execSQL(CREATE_TABLE_TYPE);
Toast.makeText(sContext,"成功创建数据表",Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists users");
db.execSQL("drop table if exists types");
onCreate(db);
}
}
添加数据
insert(String table,String nullColumnHack,ContentValus values)
更新数据
update(String table,ContenValues values,String whereClause,String where[]Args)
删除数据
delete(String table,String whereClause,String[]Args)
查询数据
query(String table,String[] columns,String selection,String[] selectionArgs,String groupBy,String having,String ordeBy,String limit)
同时也可以使用SQL命令操作数据库,例如:
myDb.execSQL(inser into users(userid,password)valus(?,?),new String[]{name,password});
到此,关于“Android中的SQLite有什么用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注天达云网站,小编会继续努力为大家带来更多实用的文章!