zoukankan      html  css  js  c++  java
  • android SQLite数据的增删改查(包括adb查询)

    首先告诉一下查询方式:

    一、配置一下sqlite3环境变量:C:UsersshaoxinAppDataLocalAndroidsdkplatform-tools

    就是把SDK中的platform-tools的路径放入path中

    二、查询代码:1、adb shell

    2、cd data/data

    3、cd com.chuanxidemo.shaoxin.demo07

    4、cd databases //找到数据库

    5、sqlite3 stuInfo.db //这是进入你创建的数据库

    6、select * from student ;  //注意这里的分号别漏了

    三、具体的代码实现:

    BaseActivity //个人比较喜欢写个基类,觉得写起代码会方便很多

    package com.chuanxidemo.shaoxin.demo07;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    
    /**
     * Created by shaoxin on 2017/2/21.
     */
    
    public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    
        
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView();
            init();
            setListener();
        }
    
        protected abstract void setContentView();
    
        protected abstract void setListener();
    
        protected abstract void init();
    
    }

    MainActivity

    package com.chuanxidemo.shaoxin.demo07;
    
    import android.content.ContentValues;
    import android.database.sqlite.SQLiteDatabase;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends BaseActivity {
        private Button cSqlite;
        private Button cTable;
        private SQLiteDatabase db;
        private Button cInsert;
        private Button cUpdata;
        private Button cDelete;
    
    
        @Override
        protected void setContentView() {
            setContentView(R.layout.activity_main);
        }
    
        @Override
        protected void setListener() {
            cSqlite.setOnClickListener(this);
            cTable.setOnClickListener(this);
            cInsert.setOnClickListener(this);
            cUpdata.setOnClickListener(this);
            cDelete.setOnClickListener(this);
        }
    
    
        @Override
        protected void init() {
            cSqlite = (Button) findViewById(R.id.c_sqlite);
            cTable = (Button) findViewById(R.id.c_table);
            cInsert = (Button) findViewById(R.id.c_insert);
            cUpdata = (Button) findViewById(R.id.c_updata);
            cDelete = (Button) findViewById(R.id.c_delete);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.c_sqlite:
                    db = openOrCreateDatabase("stuInfo.db",MODE_APPEND,null);
                    break;
                case R.id.c_table:
    
                {
                    String sql = "create table student (_id integer primary key autoincrement," +
                            "name varchar(20),age integerr)";
                    db.execSQL(sql);
                }
                    break;
                case R.id.c_insert:
                    ContentValues values = new ContentValues();
                    values.put("name","lisi");
                    values.put("age",21);
                    db.insert("student",null,values);
    //                String sql1 = "insert into student (name , age) values ('lisi', 21)";
    //                db.execSQL(sql1);
                    break;
                case R.id.c_updata:
                    String sql2 = "update student set age = 23 where _id = 1";
                    db.execSQL(sql2);
                    break;
                case R.id.c_delete:
                    String sql3 = "delete from student where _id = 1 ";
                    db.execSQL(sql3);
                    break;
            }
        }
    }

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.chuanxidemo.shaoxin.demo07.MainActivity">
    
        <Button
            android:id="@+id/c_sqlite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="创建数据库" />
    
        <Button
            android:id="@+id/c_table"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="创建表" />
    
        <Button
            android:id="@+id/c_insert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="插入数据" />
    
        <Button
            android:id="@+id/c_updata"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="更新数据" />
    
        <Button
            android:id="@+id/c_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="删除数据" />
    </LinearLayout>
  • 相关阅读:
    仿jquery 选择器功能
    多个div拖拽功能
    js 模拟jquery onready 事件
    随着鼠标移动的图片百叶窗效果
    计算体重引发的思考
    js 模拟事件
    表单验证功能(利用冒泡功能)
    视频播放滚动条(最终完善版)
    仿制视频播放滚动条效果(加左右控制按钮)
    无极树(待整理)
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/6434228.html
Copyright © 2011-2022 走看看