赞
踩
学习《第一行代码 Android (第2版)》第六章的数据库部分时遇到下列问题,特此进行记录。
Android sutdio使用命令行adb shell时无法进入root,报错su:inaccessible or not found。
无法进入root,就不能进入/data/data/*,所以需要解决这个问题。
使用命令行查看内部数据库时,出现此情况。
百度查了很多的解决方法,都不是很好用,也没有写到问题的关键。
解决方法其实非常简单,只需要使用Google APIs版本的模拟器,不要使用Google Play版本的模拟器即可。
使用下方红色框内的均可
xml代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- xmlns:android="http://schemas.android.com/apk/res/android">
-
- <Button
- android:id="@+id/button_db"
- android:layout_gravity="center"
- android:layout_width="140sp"
- android:layout_height="wrap_content"
- android:text="创建数据库"
- android:textSize="18sp"/>
- <Button
- android:id="@+id/button_add"
- android:layout_width="140sp"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="添加数据"
- android:textSize="18sp"/>
-
- </LinearLayout>

MainActivity.java代码
- package com.example.databasetest;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.content.ContentValues;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
-
- public class MainActivity extends AppCompatActivity {
-
- private MyDatabaseHelper dbHelper;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- dbHelper = new MyDatabaseHelper(this,"BookStore.db", null, 2);
- Button button_db = (Button) findViewById(R.id.button_db);
- Button button_add = (Button) findViewById(R.id.button_add);
-
- button_db.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- dbHelper.getReadableDatabase();
- }
- });
-
- button_add.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put("name", "Java");
- values.put("author", "小林");
- values.put("pages", 450);
- values.put("price", 30.20);
- db.insert("Book", null, values);
- values.clear();
-
- values.put("name", "C++");
- values.put("author", "小李");
- values.put("pages", 430);
- values.put("price", 31.20);
- db.insert("Book", null, values);
- values.clear();
- }
- });
- }
- }

MyDatabaseHelper.java文件
- package com.example.databasetest;
-
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.widget.Toast;
-
- public class MyDatabaseHelper extends SQLiteOpenHelper {
- public static final String CREATE_BOOK = "create table book("
- + "id integer primary key autoincrement, "
- + "author text, "
- + "price real, "
- + "pages integer, "
- + "name text)";
- public static final String CREATE_CATEGORY = "create table Category("
- + "id integer primary key autoincrement, "
- + "category_name text, "
- + "category_code integer)";
-
-
- private Context mContext;
-
- public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
- super(context, name, factory, version);
- mContext = context;
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(CREATE_BOOK);
- db.execSQL(CREATE_CATEGORY);
- Toast.makeText(mContext, "创建成功",Toast.LENGTH_SHORT).show();
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("drop table if exists Book");
- db.execSQL("drop table if exists Category");
- onCreate(db);
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。