SQLite

来源:互联网 发布:青云云计算 编辑:程序博客网 时间:2024/04/30 22:38

概述

SQLite 是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite 是在世界上最广泛部署的 SQL 数据库引擎。SQLite 源代码不受版权限制。

源代码

MainActivity

package com.example.administrator.db;import android.content.ContentValues;import android.content.SharedPreferences;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //写        SharedPreferences sp = getSharedPreferences("john", MODE_PRIVATE);        SharedPreferences.Editor editor = sp.edit();        editor.putString("name", "zhulihang");        editor.putInt("age", 21);        editor.putBoolean("flag", true);        editor.apply();        //读        String name = sp.getString("name", null);        int age = sp.getInt("age", -100);        Toast.makeText(this, name + " " + age, Toast.LENGTH_SHORT).show();        MySQLiteHelper sqLitehelper = new MySQLiteHelper(this, "mydb", null, 1);        final SQLiteDatabase db = sqLitehelper.getReadableDatabase();//此处会调用oncreate        Button btnAdd = ((Button) findViewById(R.id.add_user));        Button btnGet = ((Button) findViewById(R.id.get_user));        btnAdd.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                ContentValues contentvalues = new ContentValues();                contentvalues.put("name", "老王");                contentvalues.put("age", 23);                db.insert("user", null, contentvalues);            }        });        btnGet.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Cursor cursor = db.query("user", null, "id=?", new String[]{"1"}, null, null, null);                String s = "";                while (cursor.moveToNext()) {                    String name = cursor.getString(cursor.getColumnIndex("name"));                    int age = cursor.getInt(cursor.getColumnIndex("age"));                    s += name + ":" + age + "\n";                }                cursor.close();                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();            }        });    }}

MySQLiteHelper

新建一个java类,命名为MySQLiteHelper

package com.example.administrator.db;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;/** * Created by Administrator on 2017-11-11. */public class MySQLiteHelper extends SQLiteOpenHelper {    public MySQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {        super(context, name, factory, version);    }    //第一次访问数据库时会调用onCreate,只会调用一次    @Override    public void onCreate(SQLiteDatabase db) {        db.execSQL("create table user(id integer primary key autoincrement,"+                    "name text," +                    "age integer"+                    ");");    }    //数据库升级时会回调    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    }}

MainActivity

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.administrator.db.MainActivity">    <Button        android:id="@+id/add_user"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="添加User" />    <Button        android:id="@+id/get_user"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="读取User" /></LinearLayout>
原创粉丝点击