DataBase数据库学习
2016-07-10 10:27
225 查看
<?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()); } } }); } }
相关文章推荐
- mysql 使用笔记
- 导入文件数据到数据库
- MySQL基础
- Oracle行内链接不会引起USER_TABLES中CHAIN_CNT值变化
- mysql root 用户被删
- 【Mybatis框架】输入映射-pojo包装类型
- 【MyBatis框架】SqlMapConfig剖析
- Play 2.0 用户指南 - 访问SQL数据库 -- 针对Scala开发者
- scala+play+slick + mysql
- postgresql----文本搜索类型和检索函数
- 数据库原理,有深度好文
- SQL Server对文件访问的权限
- 图文详解mybatis+postgresql平台搭建步骤
- Sqlldr性能优化总结
- 启动mysql服务
- windows10安装Oracle提示错误-INS-13001环境不满足最低要求
- MySQL增删改查的常用操作指令总结
- MySQL删除表中多余的重复记录,只保留一条记录
- MySQL问题:You can't specify target table '表名' for update in FROM clause
- 从头开始搭建一个mybatis+postgresql平台