创建数据库

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);
    }

    /**
     * Called when the database is created for the first time.
     * 当数据库第一次创建的时候调用
     * 那么这个方法特别适合做表结构的初始化  创建表就是写sql语句
     */
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20))");
        
    }
    /**
     * Called when the database needs to be upgraded
     * 当数据库版本升级的时候调用 
     * 这个方法适合做   表结构的更新
     */
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("alter table info add phone varchar(20)");
        
    }

}

利用谷歌封装好的API对数据库进行增删改查

package com.itheima.sqlite;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.provider.Contacts.Intents.Insert;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.itheima.crud.R;

public class MainActivity extends Activity {

    private MyOpenHelper myOpenHelper;
    private List<Person> lists;
    private ListView lv;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //[0]找到lv
        
        lv = (ListView) findViewById(R.id.lv);
        
        myOpenHelper = new MyOpenHelper(getApplicationContext());
        
        
        //[1]定义一个集合用来存listview 要展示的数据 
        lists = new ArrayList<Person>();
        
        //打开或者创建数据库  如果是第一次就是创建   
                //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[]{"张三","1388888"});
        /**
         * table 表名  
         * ContentValues 内部封装了一个map   key:  对应列的名字  value对应的值
         */
        ContentValues values = new ContentValues();
        values.put("name", "王五");
        values.put("phone", "110");
        //返回值代表插入新行的id 
        long insert = db.insert("info", null, values); //底层就在组拼sql语句
        //[3]数据库用完需要关闭
        db.close();
        
        if (insert>0) {
            Toast.makeText(getApplicationContext(), "添加成功", 1).show();
        }else {
            Toast.makeText(getApplicationContext(), "添加fail", 1).show();
        }
        
    }
    
    
    //删除
    public void click2(View v){
        SQLiteDatabase db = myOpenHelper.getWritableDatabase();
                //db.execSQL("delete from info where name=?", new Object[]{"张三"});
        //返回值代表影响的行数 
        int delete = db.delete("info", "name=?", new String[]{"王五"});
        db.close();

        Toast.makeText(getApplicationContext(), "删除了"+delete+"行", 0).show();
        
    }
    
    //更新 
    public void click3(View v){
        SQLiteDatabase db = myOpenHelper.getWritableDatabase();

               //db.execSQL("update info set phone=? where name=? ", new Object[]{"138888888","张三"});    
        ContentValues values = new ContentValues();
        values.put("phone", "114");
        //代表更新了多少行 
        int update = db.update("info", values, "name=?", new String[]{"王五"});
        db.close();
        
        Toast.makeText(getApplicationContext(), "更新了"+update+"行", 0).show();
    }
    
    
    //查找
    public void click4(View v){
        
        SQLiteDatabase db = myOpenHelper.getReadableDatabase();

        //columns 代表你要查询的列
        //selection 根据什么查询phone 
//        Cursor cursor = db.query("info", new String[]{"name","phone"}, "name=?", new String[]{"王五"}, null, null, null);
        Cursor cursor = db.query("info", null,null, null, null, null, null);

                //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);
                
                //把数据封装到javabean
                Person person = new Person();
                person.setName(name);
                person.setPhone(phone);
                
                //把javabena对象加入到集合
                lists.add(person);
                
            }
                    
            
        }
        
        
    }        
}

最后修改:2017-10-09
如果覺得我的文章對你有用,請隨意讚賞