安卓学习Day09

今天发现data目录打不开,还遇到DDMS的端口8700被占用的情况

=================


windows下通过adb shell进入data目录

默认情况下,在cmd窗口下输入adb shell,然后执行

cd data/data进入data/data目录时会发现权限被拒绝,

并提示Permission denied。

然后:

$ ---->变成了# 代表,已经入root模式

命令行下:发现data/data 里面的文件可以访问了,但是问题是,DDMS界面上的data文件夹依然打不开

继续:命令行输入 chmod 777 /data

这时发现DDMS的data文件夹可以开启了,但是data文件夹里的还一个data文件夹无法打开

所以继续 开启它的权限 : chmod 777 /data/data

完美解决

额外知识:

知道为什么修改文件权限 是写 7 7 7 吗?

===========================

今天笔记

TextUtils.isEmpty() 一次性进行两种空值的判断
editText.setSelection(inputText.length) //输入光标移动到文本的末尾

SP:

SQLite数据库

package com.example.databasetest;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private Button addButton;
    private Button updateButton;
    private Button deleteButton;
    private Button queryButton;

    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addButton = (Button) findViewById(R.id.add_data);
        button = (Button) findViewById(R.id.create_database);
        updateButton = (Button) findViewById(R.id.update_data);
        deleteButton = (Button) findViewById(R.id.delete_data);
        queryButton = (Button) findViewById(R.id.aa);

        dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 5);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                //开始组装第一条数据
                values.put("name", "C语言");
                values.put("author", "小博士");
                values.put("page", 454);
                values.put("price", 16.96);
                db.insert("Book", null, values);//插入第一条数据
                values.clear(); //干什么的?
                //开始组装第二条数据
                values.put("name", "java语言");
                values.put("author", "大本科");
                values.put("page", 54);
                values.put("price", 56.12);
                db.insert("Book", null, values);
            }
        });
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("price", 11.11);
                db.update("Book", values, "name=?", new String[]{"C语言"});
            }
        });
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.delete("Book", "page>?", new String[]{"400"});
            }
        });

        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("aa", "Bookename is " ) ;
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("Book", null, null, null, null, null, null);
                if (cursor.moveToFirst()) {

                    do {
                        //遍历Cursor对象,取出数据并打印
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        int page = cursor.getInt(cursor.getColumnIndex("page"));
                        double price = cursor.getDouble((cursor.getColumnIndex("price")));
                        Log.d("aa", "Bookename is " + name);
                        Log.d("aa", "Bookeauthor is " + author);
                        Log.d("aa", "Bookepage is " + page);
                        Log.d("aa", "Bookprice is " + price);
                    } while (cursor.moveToNext());

                }
                cursor.close();
            }
        });
    }
}

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,"
            +"page integer,"
            + "name text)";

    public  static  final  String CREATE_CATEGORY = "create table Category ("
            + "id integer primary key autoincrement,"
            +"author text , "
            +"price real,"
            +"page integer,"
            +"name text)";
    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);  //不会再执行。因为如果此时数据库表已经存在了,那么这个onCreate()方法都不会再次执行
        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);
    }
}

原文地址:https://www.cnblogs.com/czy16/p/8643609.html