SQLite数据库存储

作者: Dream城堡 | 来源:发表于2018-11-26 11:46 被阅读12次

SQLite数据库存储

1.修改activity_main.xml:
<?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"
    tools:context="com.example.sharepreferencetest.MainActivity">


    <Button
        android:id="@+id/button"

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存数据按钮"
        android:layout_weight="1"
        />


    <Button
        android:id="@+id/get_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取出数据按钮"
        android:layout_weight="1"

        />

    <Button
        android:id="@+id/create_database"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="创建数据库"
        android:layout_weight="1"

        />


</LinearLayout>

2.建立MyDatavaseHelper:

package com.example.sharepreferencetest;


import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import java.security.interfaces.DSAKey;


public class MyDatavaseHelper extends SQLiteOpenHelper {

    private static final String CREATE_BOOK = "create table Book(\n" +
            "        id integer primary key autoincrement,\n" +
            "        author text,\n" +
            "        price real,\n" +
            "        pages integer,\n" +
            "        name text\n" +
            "        )";


    private static final String CREATE_CATEGORY = "create table Category(\n" +
            "        id integer primary key autoincrement,\n" +
            "        category_name text,\n" +
            "        category_code integer\n" +
            "        )\n";

    private Context mContext;

    public MyDatavaseHelper(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);

        System.out.println("创建了啊");
        Toast.makeText(mContext, "建表成功!", Toast.LENGTH_LONG).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);

    }
}

3.MainActivity:
package com.example.sharepreferencetest;

import android.content.Context;
import android.content.SharedPreferences;
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 MyDatavaseHelper dbHelper;

    private static final String TAG = "MainActivity";
    Context context = getBaseContext();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDatavaseHelper(this, "BookStore.db", null, 1);
        Button createDatabase = (Button) findViewById(R.id.create_database);
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
            }
        });


        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SharedPreferences.Editor edit = getSharedPreferences(
                        "data", MODE_PRIVATE
                ).edit();

                edit.putString("name", "张三");
                edit.putInt("age", 15);
                edit.putBoolean("old", false);
                edit.apply();


            }
        });

        Button getBut = (Button) findViewById(R.id.get_data);

        getBut.setOnClickListener(

                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
                        String name = pref.getString("name", null);
                        int age = pref.getInt("age", 0);
                        boolean old = pref.getBoolean("old", false);
                        Log.d(TAG, "onClick: " + name);
//                        Toast.makeText(context, name + " " + age + " " + old, Toast.LENGTH_LONG).show();
                    }

                });

    }
}

相关文章

网友评论

    本文标题:SQLite数据库存储

    本文链接:https://www.haomeiwen.com/subject/ihzvqqtx.html