|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
修改activity_main.xml文件- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <Button
- android:id="@+id/create_database"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="create database"/>
- <Button
- android:id="@+id/add_data"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Add data"/>
- <Button
- android:id="@+id/update_data"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Update data"/>
- <Button
- android:id="@+id/delete_data"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Delete data"/>
- </LinearLayout>
复制代码
修改mainactivity.java文件- package com.example.xinwei.databasetest;
- import android.content.ContentValues;
- import android.database.sqlite.SQLiteDatabase;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity {
- private MyDatabaseHelper dbHelper;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
- Button createDatabase = (Button)findViewById(R.id.create_database);
- createDatabase.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- dbHelper.getWritableDatabase();
- }
- });
- addData();
- updateData();
- deleteData();
- }
- private void deleteData() {
- Button deleteButton = (Button)findViewById(R.id.delete_data);
- deleteButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- db.delete("book","pages>?",new String[]{"500"});
- }
- });
- }
- private void updateData() {
- Button updateData = (Button)findViewById(R.id.update_data);
- updateData.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- SQLiteDatabase db=dbHelper.getWritableDatabase();
- ContentValues values=new ContentValues();
- values.put("price",10.99);
- db.update("book",values,"name=?",new String[]{"The Da Vinci Code"});
- }
- });
- }
- private void addData() {
- Button addData = (Button)findViewById(R.id.add_data);
- addData.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- SQLiteDatabase db = dbHelper.getWritableDatabase();
- ContentValues values=new ContentValues();
- values.put("name","The Da Vinci Code");
- values.put("author","Dan Brown");
- values.put("pages",454);
- values.put("price",16.96);
- db.insert("book",null,values);
- values.clear();
- values.put("name","The Lost Temple");
- values.put("author","Dan Brown");
- values.put("pages",510);
- values.put("price",19.95);
- db.insert("book",null,values);
- values.clear();
- }
- });
- }
- }
复制代码
其中updateData()函数就是把达芬奇密码这本书的价格更新为10.99,deleteData()这个函数的作用是删除所有页数大于500的书。 |
|