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导出数据库查看:

  插入成功!

posted @ 2015-10-19 15:02  li-xyz  阅读(478)  评论(0编辑  收藏  举报