Android笔记(四十) Android中的数据存储——SQLite(二) insert
准备工作:
我们模拟一个注册的页面,先看UI
我们需要创建一个数据库:user,数据库包含表user,user表包含字段id、username、password、mobilephone
MainActivity.java
package cn.lixyz.sqlitedemo; import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText username,password,againPassword,mobilephone;
private Button register; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findView(); MyDatabaseHelper mdh = new MyDatabaseHelper(MainActivity.this,"user.db",null,1);
SQLiteDatabase database = mdh.getWritableDatabase();
String dbName = mdh.getDatabaseName();
Toast.makeText(MainActivity.this,"数据库 " + dbName + " 创建成功",Toast.LENGTH_SHORT).show(); register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { }
}); } private void findView(){
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
againPassword = (EditText) findViewById(R.id.againPassword);
mobilephone = (EditText) findViewById(R.id.mobilephone);
register = (Button) findViewById(R.id.register);
}
}
MyDatabaseHelper.java
package cn.lixyz.sqlitedemo; import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast; /**
* Created by LGB on 2015/10/16.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_USER = "create table user (id integer primary key autoincrement,username text,password text,mobilephone text)";
private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USER);
Toast.makeText(mContext,"user表创建成功",Toast.LENGTH_SHORT).show();
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入您要注册的用户名" /> <EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="输入您的密码"
android:password="true"/> <EditText
android:id="@+id/againPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="确认您的密码"
android:password="true"/>
<EditText
android:id="@+id/mobilephone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入您的手机号"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="注 册"/> </LinearLayout>
我们要做的是,点击注册按钮,将用户填入的信息存入到user.user中去
添加数据:
在android中,SQLiteDatabase提供了一个insert方法,这个方法就是专门用于添加数据的
insert(String table, String nullColumnHack, ContentValues values)
第一个参数是要插入的表名,第二个参数是用于在未指定添加数据的情况下给某些可以为空的列自动赋值NULL,第三个参数是一个ContentValues对象。
ContentValues提供了一系列的put方法重载,用于向ContentValues对象中添加数据,ContentValues对象内包含一个Map对象,其key为数据库表中的列名,values为要添加的内容
例子:
MainActivity.java
package cn.lixyz.sqlitedemo; import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText ed_username,ed_password,ed_againPassword,ed_mobilephone;
private Button bt_register;
private SQLiteDatabase database; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findView(); MyDatabaseHelper mdh = new MyDatabaseHelper(MainActivity.this,"user.db",null,1);
database = mdh.getWritableDatabase();
String dbName = mdh.getDatabaseName();
Toast.makeText(MainActivity.this,"数据库 " + dbName + " 创建成功",Toast.LENGTH_SHORT).show(); bt_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues cv = new ContentValues();
String username = ed_username.getText().toString();
cv.put("username",username);
if (ed_password.getText().toString().equals(ed_againPassword.getText().toString())){
String password = ed_password.getText().toString();
cv.put("password",password);
}else{
Toast.makeText(MainActivity.this,"您的密码不一致",Toast.LENGTH_SHORT).show();
cv.clear();
return;
}
String mobilephone = ed_mobilephone.getText().toString();
cv.put("mobilephone",mobilephone);
database.insert("user", null, cv);
Toast.makeText(MainActivity.this,"插入成功",Toast.LENGTH_SHORT).show();
cv.clear();
}
}); } private void findView(){
ed_username = (EditText) findViewById(R.id.ed_username);
ed_password = (EditText) findViewById(R.id.ed_password);
ed_againPassword = (EditText) findViewById(R.id.ed_againPassword);
ed_mobilephone = (EditText) findViewById(R.id.ed_mobilephone);
bt_register = (Button) findViewById(R.id.bt_register);
}
}
MyDatabaseHelper.java
package cn.lixyz.sqlitedemo; import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast; /**
* Created by LGB on 2015/10/16.
*/
public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_USER = "create table user (id integer primary key autoincrement,username text,password text,mobilephone text)";
private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext = context;
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USER);
Toast.makeText(mContext,"user表创建成功",Toast.LENGTH_SHORT).show();
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <EditText
android:id="@+id/ed_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="请输入您要注册的用户名" /> <EditText
android:id="@+id/ed_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="输入您的密码"
android:password="true"/> <EditText
android:id="@+id/ed_againPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="确认您的密码"
android:password="true"/>
<EditText
android:id="@+id/ed_mobilephone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入您的手机号"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/bt_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="注 册"/> </LinearLayout>
运行结果:
DDMS导出数据库查看:
插入成功!
Android笔记(四十) Android中的数据存储——SQLite(二) insert的更多相关文章
- Android中的数据存储(二):文件存储 2017-05-25 08:16 35人阅读 评论(0) 收藏
文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ...
- Android笔记(四十二) Android中的数据存储——SQLite(四)update
update方法的四个参数: update()方法参数 对应的sql部分 描述 table update table_name 更新的表名 values set column=xxx ContentV ...
- Android笔记(四十四) Android中的数据存储——SQLite(六)整合
实现注册.登录.注销账户 MainActivity.java package cn.lixyz.activity; import android.app.Activity; import androi ...
- Android笔记(四十一) Android中的数据存储——SQLite(三)select
SQLite 通过query实现查询,它通过一系列参数来定义查询条件. 各参数说明: query()方法参数 对应sql部分 描述 table from table_name 表名称 colums s ...
- Android笔记(四十三) Android中的数据存储——SQLite(五)delete
SQLite通过delete()方法删除数据 delete()方法参数说明: delete()方法参数 对应sql部分 描述 table delte from table_name 要删除的表 whe ...
- Android笔记(三十九) Android中的数据存储——SQLite(一) create
SQLite是内置于Android的一款轻量级关系型数据库,她运算速度快,占用资源少,通常只需要几百K的内存就足够了,因而特别适合在移动设备上使用. SQLite不仅支持标准的SQL语法,还遵循数据库 ...
- Android笔记(三十八) Android中的数据存储——SharedPreferences
SharedPreferences是Android提供的一种轻型的数据存储方法,其本质是基于xml文件存储的,内部数据以key-value的方式存储,通常用来存储一些简单的配置信息. SharedPr ...
- 67.Android中的数据存储总结
转载:http://mp.weixin.qq.com/s?__biz=MzIzMjE1Njg4Mw==&mid=2650117688&idx=1&sn=d6c73f9f04d0 ...
- Android中的数据存储
Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...
随机推荐
- Oracle系列四 单行函数查询语句
单行函数 操作数据对象 接受参数返回一个结果 只对一行进行变换 每行返回一个结果 可以转换数据类型 可以嵌套 参数可以是一列或一个值 包含:字符,数值,日期,转换,通用 字符函数 1.大小写控制函数: ...
- (2)PyCharm开发Flash项目之蓝图构建
下面通过在PyCharm开发工具中创建一个简单的Flask项目来体会一下Flask的蓝图构建(Blueprint). 何谓蓝图:在Flask中蓝图就在大型应用中,将不同功能的模块(module)分开管 ...
- ISO/IEC 9899:2011 条款6.10.3——宏替换
6.10.3 宏替换 约束 1.两个替换列表是相同的,当且仅当两个替换列表中的预处理符记都具有相同的数.次序.拼写,以及空白分隔符,这里所有的空白分隔符都认为是相同的. 2.当前被定义为一个类似对象的 ...
- [LeetCode] 636. Exclusive Time of Functions 函数的独家时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- LeetCode:缺失的第一个正数【41】
LeetCode:缺失的第一个正数[41] 题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3示例 2: 输入: [3,4,-1,1] ...
- 进程间之异步通信:信号Signal
信号 信号是进程间通信机制中唯一的异步通信机制:信号机制是进程间传递消息的一种机制,是异步进程中通信的一种方式 一个进程一旦接收到信号就会打断原来的程序执行流程来处理信号 内核处理一个进程收到的软中断 ...
- mysql创建用户并授权Repl_slave_priv和Repl_client_priv
CREATE USER 'test'@'localhost' IDENTIFIED BY 'test'; FLUSH PRIVILEGES; GRANT REPLICATION CLIENT ON * ...
- web自动化测试笔记(二)
如何使用selenium工具 上章节介绍了搭建web自动化的环境,这个章节介绍如何使用selenium写自动化脚本. 1.selenium selenium是一个用于web应用程序的测试工具.它可以帮 ...
- [转帖]keepalived实现服务高可用
keepalived实现服务高可用 https://www.cnblogs.com/clsn/p/8052649.html 第1章 keepalived服务说明 1.1 keepalived是什么? ...
- Django框架3——模型
Django数据库层解决的问题 在本例的视图中,使用了pymysql 类库来连接 MySQL 数据库,取回一些记录,将它们提供给模板以显示一个网页: from django.shortcuts imp ...