设为首页 加入收藏

TOP

安卓开发笔记(十三):SQLite数据库储存(下)数据的增添,更改,删除,查询(三)
2019-09-01 23:12:59 】 浏览:79
Tags:安卓 开发 笔记 十三 SQLite 数据库 储存 数据 增添 更改 删除 查询
t;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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/creat" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Create database"/> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add data"/> <Button android:id="@+id/updata" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Updata data"/> <Button android:id="@+id/deletedata" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Delete data"/> </LinearLayout>

 

然后写入主活动的代码:

package com.example.lenovo.studyittwo;


import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.*;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 构建MyDatabaseHelper对象,指定数据库名为"BookStore.db、版本号为1,版本号改为2之后则会直接
        dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 2);
        Button btn_create_database = (Button) findViewById(R.id.creat);
        btn_create_database.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 创建或打开一个现有的数据库(已存在则打开,否则创建一个新的)
                dbHelper.getWritableDatabase();
            }
        });
        Button addData= (Button)findViewById(R.id.add);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               SQLiteDatabase db=dbHelper.getWritableDatabase();
               ContentValues values=new ContentValues();
               values.put("name","the fuck code");
               values.put("autuor","fuckers");
               db.insert("Book",null,values);
               values.clear();
               values.put("name","the fuck code");
               values.put("autuor","fuckers");
               db.insert("Category",null,values);
               values.clear();

            }
        });
        Button updataData= (Button)findViewById(R.id.updata);
        updataData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               SQLiteDatabase db=dbHelper.getWritableDatabase(
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇分享 Xamarin.android 关于使用SQ.. 下一篇Android自动解析html带图片,实现..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目