首页 > 代码库 > SQLite数据库

SQLite数据库

个人学习,仅供参考:

layout部分:

技术分享

 

<?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:id="@+id/activity_main"
    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.example.administrator.mysqlite2.MainActivity">

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:background="@color/colorPrimary"
        />

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:background="@color/colorAccent"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:layout_marginTop="30dp"
        android:text="注册"
        android:onClick="addUser"
        android:background="@drawable/shape"/>
    <Button
        android:id="@+id/del"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:layout_marginTop="30dp"
        android:text="删除"
        android:onClick="del"

        android:background="@drawable/shape"/>
    <Button
        android:id="@+id/update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:layout_marginTop="30dp"
        android:text="更新"
        android:onClick="update"

        android:background="@drawable/shape"/>
    <Button
        android:id="@+id/query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:layout_marginTop="30dp"
        android:text="查询"
        android:onClick="query"

        android:background="@drawable/shape"/>
</LinearLayout>
控件附加部分:

技术分享

model类部分:

技术分享

创建数据库部分:

package com.example.administrator.mysqlite2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 构造方法
 * @param context:上下文
//     * @param name:数据库名称
//     * @param factory:游标工厂,默认为null
//     * @param version:数据库版本号,大于等于1
 */

public class MyDatabaseSqlite extends SQLiteOpenHelper {

    public MyDatabaseSqlite(Context context) {
        super(context, "user2.db", null , 3);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table userLiang(id integer primary key autoincrement,name varchar(20),password varchar(20))");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("alter table userLiang add sex varchar(20)");
    }
}
---------------------

对数据库的增删改查方法:SQLDate

package com.example.administrator.mysqlite2;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * Created by Administrator on 2017/1/4.
 */

public class SQLDate {

    private MyDatabaseSqlite databaseSqlite;



    public SQLDate(Context context) {
        databaseSqlite=new MyDatabaseSqlite(context);
    }
//第一种方法
   /* public void add(String name,String password){
        //对数据库进行读操作
       SQLiteDatabase db=databaseSqlite.getWritableDatabase();
        db.execSQL("insert into userLiang(name,password)Values(?,?)",new Object[]{name,password});
    }*/
   /* //第二种添加数据方法
    public void add(String name,String password){
        SQLiteDatabase db=databaseSqlite.getWritableDatabase();
        //盛放数据的集合
        ContentValues cv=new ContentValues();
        cv.put("name",name);
        cv.put("password",password);
        db.insert("userLiang",null,cv);
        db.close();
    }*/
    //第三种方式添加数据
    public void add(UserModel model){
        SQLiteDatabase db=databaseSqlite.getWritableDatabase();
        //向数据库中添加数据
        db.execSQL("insert into userLiang(name,password)Values(?,?)",new Object[]{model.getName(),model.getPassword()});

    }

    //删
   /* public void delete(String name){
        SQLiteDatabase db=databaseSqlite.getWritableDatabase();
        //删除数据
        db.execSQL("delete from userLiang where name=?",new Object[]{name});
    }*/
      public void delete(String name){
        SQLiteDatabase db=databaseSqlite.getWritableDatabase();
        //删除数据
        db.delete("user2","name=?",new String[]{name});
    }
    //改
    /*public void update(String name,String password){
        SQLiteDatabase db=databaseSqlite.getWritableDatabase();
        db.execSQL("update userLiang set password=? where name=?",new Object[]{password,name});
    }*/
    public void update(String name,String password){
        SQLiteDatabase db=databaseSqlite.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("name",name);
        cv.put("password",password);
        db.update("user2",cv,"name=?",new String[]{name});
        db.close();
    }

    //查
    public boolean quary(String name,String password){
        SQLiteDatabase db=databaseSqlite.getWritableDatabase();
        //查找数据
        Cursor cursor=db.rawQuery("select * from userLiang where name=? and password=?",new String[]{name,password});
        if(cursor.moveToFirst()==true){
            cursor.close();
            return true;
        }
        return false;
    }
}
--------------------

activity类:对数据的增删改查实现

package com.example.administrator.mysqlite2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import static com.example.administrator.mysqlite2.R.id.username;

public class MainActivity extends AppCompatActivity {
    private MyDatabaseSqlite myDatabaseSqlite;
    private EditText name;
    private  EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name= (EditText) findViewById(username);
        password= (EditText) findViewById(R.id.password);

       //初始化数据库
//        myDatabaseSqlite=new MyDatabaseSqlite(this);
      //获取数据库读写权限
//        SQLiteDatabase sqLiteDatabase=myDatabaseSqlite.getWritableDatabase();
      /*  //获取数据库的只读权限
        SQLiteDatabase sqLiteDatabase1=myDatabaseSqlite.getReadableDatabase();*/
    }
//第二种
 /*   public void addUser(View view){
        SQLDate modelDate=new SQLDate(this);
        String name1= name.getText().toString();
        String pass=password.getText().toString();
        modelDate.add(name1,pass);
    }*/
//第三种
    public void addUser(View view){
        SQLDate model=new SQLDate(this);
        UserModel userModel=new UserModel();
        String name1=name.getText().toString();
        String pass=password.getText().toString();
        userModel.setName(name1);
        userModel.setPassword(pass);
        model.add(userModel);

    }
    //删除数据1
    public void del(View view){
        SQLDate model=new SQLDate(this);
        model.delete("liangli");

    }
    //更新数据
    public void update(View view){
        SQLDate model=new SQLDate(this);
        model.update("second","aaa");
    }
    //查询数据
    public void query(View view){
        SQLDate model=new SQLDate(this);
        String username=name.getText().toString();
        String pass=password.getText().toString();
        boolean flag=model.quary(username,pass);
        if(flag==true){
            Toast.makeText(this,"已找到",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(this,"未找到",Toast.LENGTH_SHORT).show();
        }
    }
}

 

SQLite数据库