Android是有自带的数据库的
原生数据库
public class MySQLiteHelper extends SQLiteOpenHelper {
private static final String TAG = "MySQLiteHelper";
//数据库建表语句
public static final String sql = "sql语句";
public static final String sql1 = "sql语句";
public MySQLiteHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);//创建数据库调用方法
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(TAG, "onCreate: " );
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i(TAG, "onUpgrade: " );
switch (oldVersion){
case 1:
db.execSQL(sql1);
break;
}
}
}
创建一个数据库:名称sjk 版本号 1
MySQLiteHelper sjk= new MySQLiteHelper(context, "sjk", null, 1);
打开数据库并进行写入操作
SQLiteDatabase db= sjk.getWritableDatabase();
增删改查跟正常的sql语句一样
查询
Cursor cursor = sqldb.rawQuery("select语句", null);
while (cursor.moveToNext()) {
//cursor.getString(0);
}
执行sql
db.execSQL(sql);
关闭
db.close();
晓雨哥哥写代码
原创文章 25获赞 118访问量 7400
关注
私信
展开阅读全文
作者:晓雨哥哥写代码