前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SQLite数据库存储

SQLite数据库存储

作者头像
Dream城堡
发布2018-12-18 17:16:07
1.9K0
发布2018-12-18 17:16:07
举报
文章被收录于专栏:Spring相关Spring相关

SQLite数据库存储

1.修改activity_main.xml:
代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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:
代码语言:javascript
复制
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();
                    }

                });

    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SQLite数据库存储
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档