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 ...
随机推荐
- Laravel 入门常见问题汇总
一.安装完成后想打开 Laravel 内置的登录页面,报错 解决方法: Laravel 利用 PHP5.4 的新特性 trait 内置了非常完善好用的简单用户登录注册功能,适合一些不需要复杂用户权限管 ...
- QStackedWidget居中布局
QStackedWidget* m_pStackedWidget= new QStackedWidget(this); QWidget* btnWidget = new QWidget; QWidge ...
- SpringBoot 使用AOP记录接口访问日志
文章来源:https://macrozheng.github.io/mall-learning/#/technology/aop_log AOP AOP为Aspect Oriented Program ...
- [LeetCode] 452. Minimum Number of Arrows to Burst Balloons 最少箭数爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 【Luogu P2765】魔术球问题
Luogu P2765 一开始看到这道题完全想不到怎么做,绞尽脑汁也想不到怎么去构造这个网络流模型. 于是查看了多篇题解--学习了多篇题解的讲解,终于找到了思路. 本文参考了洛谷 这一道题的题意并不难 ...
- GroupBy之后加ToList和不加ToList有什么区别吗?
class Program { static void Main(string[] args) { List<Person> ...
- Windows 下删除 Docker 容器的方法
Issue: 删除命令执行失败 如果在 CMD 命令提示符下删除容器可能失败,可切换至 PowerShell 中执行成功. unknown shorthand flag: 'a' in -a See ...
- 端口占用问题:java.net.BindException: Address already in use: bind
解决方法 方法一:换一个端口 若仍然想要使用该端口,则可以将占用该端口的进程杀死即可. 方法二:杀死占用该端口的进程 若仍然想要使用该端口,则可以将占用该端口的进程杀死即可 查找端口被占用的进程id ...
- kubernetes 实践一:基本概念和架构
这里记录kubernetes学习和使用过程中的内容. CentOS7 k8s-1.13 flanneld-0.10 docker-18.06 etcd-3.3 kubernetes基本概念 kuber ...
- Quartus II——工程建立和常用设置
Quartus ii是针对Altera FPGA的一款EDA软件,在此以一个led闪烁工程来简单说一下基本操作: 一.注意事项 Quartus ii最大的注意事项就一点:工程名称以及工程里面的文件名称 ...