SQLite存储

数据库的增删查改的功能实现模板

连接数据库,首先得在Java下新建一个.db包,在新建的db包下新建一个.db文件

DbHelper.java的代码如下:

package com.yindan.db;  
  
  
import android.content.Context;  
import android.database.sqlite.SQLiteDatabase;  
import android.database.sqlite.SQLiteOpenHelper;  
import android.util.Log;  
  
/**  
 * Created by Administrator on 2017/6/25 0025.  
 */  
  
public class DbHelper extends SQLiteOpenHelper {  
  
    /**  
     *  
     * @param context   上下文  
     * @param name       数据库的名字,“文件名.db”  
     * @param factory   数据库工厂,null  
     * @param version   数据库版本  
     */  
    public DbHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {  
        super(context, name, factory, version);  
        Log.i("test","构造方法");  
    }  
  
    //使用该方法,创建表  
    //只会调用一次  
    @Override  
    public void onCreate(SQLiteDatabase db) {  
        Log.i("test","创建表");  
        db.execSQL("create table login (uid integer primary key autoincrement,uname,upass)");  
    }  
  
    //数据库版本,低-------》高  
    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
        Log.i("test","升级数据库版本");  
  
    }  
  
    //数据库版本   高----------》低  
//    @Override  
//    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
//        super.onDowngrade(db, oldVersion, newVersion);  
//    }  
}  
``xml代码`<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:app="http://schemas.android.com/apk/res-auto"  
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="vertical"  
    tools:context="com.example.cookie.android0625sqlite.MainActivity">  
  
    <EditText  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:hint="请输入用户名"  
        android:id="@+id/et_main_uname"/>  
    <EditText  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:hint="请输入密码"  
        android:id="@+id/et_main_upass"/>  
    <Button  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:onClick="operation"  
        android:text="操作"/>  
  
</LinearLayout>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值