鱼C论坛

 找回密码
 立即注册
查看: 1591|回复: 1

[学习笔记] android编程6.4.6

[复制链接]
发表于 2017-10-20 06:30:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
修改activity_main.xml文件
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">

  6.     <Button
  7.         android:id="@+id/create_database"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:text="create database"/>
  11.     <Button
  12.         android:id="@+id/add_data"
  13.         android:layout_width="match_parent"
  14.         android:layout_height="wrap_content"
  15.         android:text="Add data"/>
  16.     <Button
  17.         android:id="@+id/update_data"
  18.         android:layout_width="match_parent"
  19.         android:layout_height="wrap_content"
  20.         android:text="Update data"/>
  21.     <Button
  22.         android:id="@+id/delete_data"
  23.         android:layout_width="match_parent"
  24.         android:layout_height="wrap_content"
  25.         android:text="Delete data"/>
  26.     <Button
  27.         android:id="@+id/query_data"
  28.         android:layout_width="match_parent"
  29.         android:layout_height="wrap_content"
  30.         android:text="Query data"/>

  31. </LinearLayout>
复制代码

修改mainactivity.java文件
  1. package com.example.xinwei.databasetest;

  2. import android.content.ContentValues;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.support.v7.app.AlertDialog;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;

  11. public class MainActivity extends AppCompatActivity {
  12.     private MyDatabaseHelper dbHelper;
  13.     static String TAG="MainActivity";
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_main);
  18.         dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
  19.         Button createDatabase = (Button)findViewById(R.id.create_database);
  20.         createDatabase.setOnClickListener(new View.OnClickListener() {
  21.             @Override
  22.             public void onClick(View view) {
  23.                 dbHelper.getWritableDatabase();
  24.             }
  25.         });
  26.         addData();
  27.         updateData();
  28.         deleteData();
  29.         queryData();
  30.     }

  31.     private void queryData() {
  32.         Button queryData = (Button)findViewById(R.id.query_data);
  33.         queryData.setOnClickListener(new View.OnClickListener() {
  34.             @Override
  35.             public void onClick(View view) {
  36.                 SQLiteDatabase db = dbHelper.getWritableDatabase();
  37.                 Cursor cursor = db.query("book",null,null,null,null,null,null);
  38.                 if(cursor.moveToFirst()) {
  39.                     do {
  40.                         String name=cursor.getString(cursor.getColumnIndex("name"));
  41.                         String author=cursor.getString(cursor.getColumnIndex("author"));
  42.                         int pages=cursor.getInt(cursor.getColumnIndex("pages"));
  43.                         double price=cursor.getDouble(cursor.getColumnIndex("price"));
  44.                         Log.d(TAG,"book name is"+name);
  45.                         Log.d(TAG,"book author is"+author);
  46.                         Log.d(TAG,"book pages is"+pages);
  47.                         Log.d(TAG,"book price is"+price);
  48.                     } while (cursor.moveToNext());
  49.                 }
  50.             }
  51.         });
  52.     }

  53.     private void deleteData() {
  54.         Button deleteButton = (Button)findViewById(R.id.delete_data);
  55.         deleteButton.setOnClickListener(new View.OnClickListener() {
  56.             @Override
  57.             public void onClick(View view) {
  58.                 SQLiteDatabase db = dbHelper.getWritableDatabase();
  59.                 db.delete("book","pages>?",new String[]{"500"});
  60.             }
  61.         });
  62.     }

  63.     private void updateData() {
  64.         Button updateData = (Button)findViewById(R.id.update_data);
  65.         updateData.setOnClickListener(new View.OnClickListener() {
  66.             @Override
  67.             public void onClick(View view) {
  68.                 SQLiteDatabase db=dbHelper.getWritableDatabase();
  69.                 ContentValues values=new ContentValues();
  70.                 values.put("price",10.99);
  71.                 db.update("book",values,"name=?",new String[]{"The Da Vinci Code"});
  72.             }
  73.         });
  74.     }

  75.     private void addData() {
  76.         Button addData = (Button)findViewById(R.id.add_data);
  77.         addData.setOnClickListener(new View.OnClickListener() {
  78.             @Override
  79.             public void onClick(View view) {
  80.                 SQLiteDatabase db = dbHelper.getWritableDatabase();
  81.                 ContentValues values=new ContentValues();
  82.                 values.put("name","The Da Vinci Code");
  83.                 values.put("author","Dan Brown");
  84.                 values.put("pages",454);
  85.                 values.put("price",16.96);
  86.                 db.insert("book",null,values);
  87.                 values.clear();
  88.                 values.put("name","The Lost Temple");
  89.                 values.put("author","Dan Brown");
  90.                 values.put("pages",510);
  91.                 values.put("price",19.95);
  92.                 db.insert("book",null,values);
  93.                 values.clear();
  94.             }
  95.         });
  96.     }
  97. }
复制代码

里面的queryData()函数就是查询的方法,点一下query data按钮在控制台就会查询到数据了 asdsaad.png

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-5-15 07:31:21 | 显示全部楼层
看来还有很多东西要学习啊!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 16:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表