数据库的创建

定义一个类继承SQLiteOpenHelper

package com.itheima.sqlite;

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

public class MyOpenHelper extends SQLiteOpenHelper {
    /**
     * context  上下文
     * name:数据库的名字 
     * factory 目的创建cursor对象   
     * version 数据库的版本   从1开始
     */
    public MyOpenHelper(Context context) {
        super(context, "itheima.db", null,1);        
    }
    /**
     * 当数据库第一次创建的时候调用
     * 那么这个方法特别适合做表结构的初始化  创建表就是写sql语句
     */
    public void onCreate(SQLiteDatabase db) {
        //id 一般以_id 
        db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20))");
    }
    /**
     * 当数据库版本升级的时候调用 
     * 这个方法适合做   表结构的更新
     */
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("alter table info add phone varchar(20)");
    }

}

安卓利用SQL语句进行增删改查

package com.itheima.sqlite;

import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
private MyOpenHelper myOpenHelper;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myOpenHelper = new MyOpenHelper(getApplicationContext());
    //打开或者创建数据库  如果是第一次就是创建   
    //SQLiteDatabase sqLiteDatabase = myOpenHelper.getWritableDatabase();
    //打开或者创建数据库  如果是第一次就是创建     如果磁盘满了 返回只读的
    //SQLiteDatabase readableDatabase = myOpenHelper.getReadableDatabase();
        
}

//点击按钮增加一条记录
public void click1(View v){
    //[1]获取数据库对象
    SQLiteDatabase db = myOpenHelper.getWritableDatabase();
    //[2]执行增加一条的sql语句 
    db.execSQL("insert into info(name,phone) values(?,?)", new Object[]{"小鸣","19189897878"});
    //[3]数据库用完需要关闭
    db.close();
    
}


//删除
public void click2(View v){
    SQLiteDatabase db = myOpenHelper.getWritableDatabase();
    db.execSQL("delete from info where name=?", new Object[]{"小鸣"});
    db.close();
    
}

//更新 
public void click3(View v){
    SQLiteDatabase db = myOpenHelper.getWritableDatabase();

    db.execSQL("update info set phone=? where name=? ", new Object[]{"1910000000","小鸣"});
    
    db.close();
}

//查找
public void click4(View v){
    SQLiteDatabase db = myOpenHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from info", null);
    if (cursor!= null&&cursor.getCount()>0) {
        while(cursor.moveToNext()){
            //columnIndex代表列的索引 
            String name = cursor.getString(1);
            String phone = cursor.getString(2);
            System.out.println("name:"+name+";phone:"+phone);    
        }
    }
    
    
}

}

附:布局文件
<Button

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="click1"
    android:text="add" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="click2"
    android:text="delete" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="click3"
    android:text="update" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="click4"
    android:text="find" />


Last modification:October 9, 2017
If you think my article is useful to you, please feel free to appreciate