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 ...
随机推荐
- np.meshgrid
- python读取yaml文件,在unittest中使用
python读取yaml文件使用,有两种方式: 1.使用ddt读取 2,使用方法读取ddt的内容,在使用方法中进行调用 1.使用ddt读取 @ddt.ddt class loginTestPage(u ...
- LLBLGen update table with join
Table1 id Name 1 xxx 2 ooo Table2 Table1Id Table1Name Column1 Column2 Column3 1 sss xxxx xxxx xxxx 2 ...
- 常用的python的内置库或者第三方库
内置库:re,json,time,random,sys,os, 第三方库:转载: https://www.cnblogs.com/jiangchunsheng/p/9275881.htmlReques ...
- PHP设计模式 - 建造者模式
建造者模式主要在于创建一些复杂的对象.将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示的设计模式; 结构图: <?php /** * * 产品本身 */ class Pro ...
- 安装donkeyid
cd /usr/local/php/include/php/ext sudo git clone https://github.com/osgochina/donkeyid.git cd /usr/l ...
- 函数的学习2——返回值&传递列表——参考Python编程从入门到实践
返回值 函数并非总是直接显示输出,相反,它可以处理一些数据,并返回一个或一组值.函数的返回值被称为返回值. 1. 简单的返回值 def get_formatted_name(first_name, l ...
- flask框架(八)—自定义命令flask-script、多app应用、wtforms表单验证、SQLAIchemy
自定义命令flask-script 用于实现类似于django中 python3 manage.py runserver ...类似的命令,用命令行启动项目 首先安装:pip3 install fla ...
- quartz2.3.0(三)cron定义调度周期
cron总结 cron详解参见:<quartz CronExpression表达式> CronTrigger配置完整格式为7个: [秒] [分] [小时] [日] [月] ...
- [洛谷P5329][SNOI2019]字符串
题目大意:给一个长度为$n$的字符串$s$,字符串$p_i$为字符串$s$去掉第$i$个字符后形成的字符串.请给所有字符串$p_i$排序(相同字符串按编号排序) 题解:先去掉所有连续相同字符,因为它们 ...