DataBase数据库学习
来源:互联网 发布:淘宝客好做不 编辑:程序博客网 时间:2024/06/07 18:01
<?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="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.user.android2lesson_04_database.MainActivity"> <Button android:id="@+id/create" android:text="创建表" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/insertButton" android:text="添加数据" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/delete" android:text="删除数据" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/updataButton" android:text="修改数据"/> <Button android:id="@+id/selectButton" android:text="查询数据" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>package com.user.android2lesson_04_database;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;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;public class MainActivity extends AppCompatActivity { /** * 数据库--数据的仓库 * 数据持久化本质是将内存的数据写入到文件中,下次运行的时候可以直接读取 * 从而达到数据的长时间存储 * 数据库就是一套软件,实现了对数据的存储 * 是通过一些指令(SQL语句)的方式来进行的 * 现在主流的数据库是关系型数据库,可以简单的理解为表格。 * 数据库按处理的数据量大小分三类 * 大型数据库:Oracle DB2 * 中型数据库:SQLServer * 小型数据库:MYSQL NOSQL * * 手机端数据库:SQLite * 几个关键字: * 表--表格 * 字段 --就是列 * 数据 --就是行 * 外键 --与其他表之间的关系 * 主键 --唯一且不为空的序列号 * 非空 --不能为空 * 唯一 -- * 有序 --按顺序排列 * 自增 --自动增长的序列 * * SQL语句简介 * 数据库永远离不开的四个操作:增删改查CRUD * create 创建数据库--创建表格 * create table student (name text ,sex text,age integer); * * insert 添加数据 * insert into student(name,sex,age) values ('张三','男',21); SQL语句都是单引号, * * delete 删除数据 * delete from student where name = '张三'; * delete from student where 1 = 1;全删 * * updata 修改数据 * updata student set name = '李四' where id =3; * * select 查询数据 * select * from student where name = '张三'; * 等同于select name,age,sex from student where name = '张三'; * * * SQLite是一种无数据类型的数据库 * 五种亲缘数据类型 * 1.Integer 整型 * 2.Varchar 包含字符串文本 * 3.NONE 二进制 * 4.REAL 浮点型 * 5.NUMERIC 其他的类型 * @param savedInstanceState */ private Button createTable; private Button insert; private Button delete; private Button updata; private Button select;// 数据库对象,代码中所有的和数据库进行的交互都是通过这个对象来进行的 private SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); createTable = (Button) findViewById(R.id.create); insert = (Button) findViewById(R.id.insertButton); delete = (Button) findViewById(R.id.delete); updata = (Button) findViewById(R.id.updataButton); select = (Button) findViewById(R.id.selectButton);// 数据库的使用// 使用之前需要创建数据库对象// 参数1:数据存储的文件位置// 参数2:文件创建工厂类,这里不需要,写为空 db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.user.android2lesson_04_database/database.db",null);// 添加响应事件// 创建表 createTable.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {// 这里进行创建表操作// 1.拼接sql语句 String sql = "CREATE TABLE IF NOT EXISTS student ('sid' INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, sex TEXT, age INTEGER);";// 向数据库对象发送SQL指令 db.execSQL(sql); Toast.makeText(getApplicationContext(),"创建表成功",Toast.LENGTH_SHORT).show(); } });// 添加数据 insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String sql = "INSERT INTO student(name,sex,age) VALUES('李三炮','男',99);"; db.execSQL(sql); Toast.makeText(getApplicationContext(),"添加数据成功",Toast.LENGTH_SHORT).show(); } });// 删除数据 delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String sql = "DELETE FROM student where sid = 1;"; db.execSQL(sql); Toast.makeText(getApplicationContext(),"删除数据成功",Toast.LENGTH_SHORT).show(); } }); updata.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String sql = "UPDATA student set name = '刘铁锤' where sid = 2;"; Toast.makeText(getApplicationContext(),"更新数据成功",Toast.LENGTH_SHORT).show(); } }); select.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {// 1.获取数据库使用的游标 Cursor cursor = db.query("student",null,null,null,null,null,null); Log.e("123", cursor.getCount() + "");// 移动到第一行,如果返回为false,那么证明没有数据 if (cursor.moveToFirst()){ // 2.循环显示数据 do {// 开始循环获取数据// 移动到第i行// cursor.move(i);// 获取ID int sid = cursor.getInt(0);// 获取名字 String name = cursor.getString(1);// 获取性别 String sex = cursor.getString(2);// 获取年龄 int age = cursor.getInt(3); Log.e("111",sid+":" +name+":"+sex+":"+age); }while(cursor.moveToNext()); } } }); }}
0 0
- DataBase数据库学习
- 数据库学习之CREATE DATABASE
- 数据库学习笔记–Database Design(1)
- H2 database 内存数据库 学习资料
- 数据库(Database)
- Music -Database -4(数据库触发器和View的学习)
- 数据库脚本:Database.sql
- “数据库营销(Database Marketing) ”
- 数据库-备份Sybase DataBase
- 手工创建数据库DATABASE
- sql 数据库(database)
- database principle (数据库原理)
- create database 创建数据库
- drop database 删除数据库
- 数据库设计(Database Design)
- 管理数据库Database
- H2 DataBase 数据库操作
- 修改数据库---ALTER DATABASE
- xcode制作framework的脚本问题
- hdu 5084 HeHe(暴力/找规律)
- 310. Minimum Height Trees
- 满二叉树和完全二叉树
- js封装表单通用函数
- DataBase数据库学习
- 统计及其学习概念解析001
- LightOJ - 1067 Combinations(快速幂+逆元)
- 快速排序
- 书架_纪中2931_dp_堆_单调队列
- SAE Python 后台1
- 【MyBatis框架】高级映射-一对多查询
- cscope.exe程序的base64文本
- JAVA代理模式--静态代理