这篇文章将为大家详细讲解有关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的作用是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:http://blog.itpub.net/69917874/viewspace-2680047/