前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SQLite的增删改查

SQLite的增删改查

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

SQLite的增删改查

1.新建一个SqliteTest
2.新建MyDatavaseHelper类:
package com.example.sqlitetest;


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


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);

        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.修改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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical"
    tools:context="com.example.sqlitetest.MainActivity">

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

        />


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

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:gravity="center_vertical"

        android:text="添加数据"
        />

    <Button
        android:id="@+id/update_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="修改数据"
        />


    <Button
        android:id="@+id/delete_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="删除数据"
        />



    <Button
        android:id="@+id/query_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"
        android:text="查询数据"
        />


</LinearLayout>
4.修改MainActivity
package com.example.sqlitetest;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.icu.text.LocaleDisplayNames;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.sql.SQLData;

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 addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                //第二种方法加入数据
                db.execSQL("insert into Book(name,author,pages,price) values(?,?,?,?)",
                        new String[]{
                                "oiujhk", "jhjh", "454", "165"
                        }
                );
                //第二种方法加入数据


                ContentValues values = new ContentValues();
                //开始组装第一条数据
                values.put("name", "kkk");
                values.put("author", "dan dd");
                values.put("pages", 454);
                values.put("price", 16.96);
                db.insert("Book", null, values);//插入第一条数据
                values.clear();
                // Toast.makeText(context,"增加数据成功",Toast.LENGTH_LONG).show();
                //开始第二条数据
                values.put("name", "反而费");
                values.put("author", "dan dd");
                values.put("pages", 555);
                values.put("price", 1665);
                db.insert("Book", null, values);//插入第二条数据

                Log.d(TAG, "添加成功!");
            }
        });

        /**
         * 修改数据
         */

        Button updData = (Button) findViewById(R.id.update_data);

        updData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                //开始组装第一条数据
                values.put("price", 45);
                db.update("Book", values, "name= ?", new String[]{
                        "kkk"
                });//修改第一条数据

                Log.d(TAG, "数据修改成功");


            }
        });


        /**
         * 删除数据
         */
        Button delData = (Button) findViewById(R.id.delete_data);

        delData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                db.delete("Book", "pages>?", new String[]{
                        "500"
                });

                Log.d(TAG, "删除成功");
            }
        });


        /**
         * 查询数据
         */
        Button queryData = (Button) findViewById(R.id.query_data);

        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                //第二种查询方法
                Cursor cursor1 = db.rawQuery("select * from Book", null);

                //查询Book中的所有的数据
                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 pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));

                        Log.d(TAG, "name:" + name);
                        Log.d(TAG, "author:" + author);
                        Log.d(TAG, "pages:" + pages);
                        Log.d(TAG, "price:" + price);
                    } while (cursor.moveToNext());
                }

                cursor.close();

                Log.d(TAG, "查询成功");
            }
        });

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SQLite的增删改查
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档