SQLite基本用法

1.MainActivity.java

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button create_database = null;
    private Button update_database = null;
    private Button insert = null;
    private Button update = null;
    private Button query = null;
    private Button delete = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        create_database = (Button)findViewById(R.id.create_database);
        create_database.setOnClickListener(new CreateDatabaseOnClickListener());
        update_database = (Button)findViewById(R.id.update_database);
        update_database.setOnClickListener(new UpdateDatabaseOnClickListener());
        insert = (Button)findViewById(R.id.insert);
        insert.setOnClickListener(new InsertOnClickListener());
        update = (Button)findViewById(R.id.update);
        update.setOnClickListener(new UpdateOnClickListener());
        query = (Button)findViewById(R.id.query);
        query.setOnClickListener(new QueryOnClickListener());
        delete = (Button)findViewById(R.id.delete);
        delete.setOnClickListener(new DeleteOnClickListener());
    }

    public class CreateDatabaseOnClickListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            //创建一个DatabaseHelper类的对象,该类是单独一个java文件,这里采用2个参数的构造函数,建立的数据
            //库的名字为tornadomeet.db
            DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
            //只有调用getReadableDatabase()或者getWriteableDatabase()函数后才能返回一个SQLiteDatabase对象
            SQLiteDatabase db = database_helper.getReadableDatabase();
        }                
    }
    
    public class UpdateDatabaseOnClickListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db", 2);
            SQLiteDatabase db = database_helper.getReadableDatabase();
        }        
    }
    
    public class InsertOnClickListener implements OnClickListener{

        public void onClick(View v) {
            // 生成contentvallues对象,该对象用来存数据的
            ContentValues values = new ContentValues();
            values.put("id", 1);//注意值的类型要匹配
            values.put("name", "tornado");
            DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
            SQLiteDatabase db = database_helper.getWritableDatabase();//这里是获得可写的数据库
            db.insert("user1", null, values);
        }       
    }
    
    public class UpdateOnClickListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
            SQLiteDatabase db = database_helper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("name", "tornadomeet");
            //参数1为表名,参数2为更新后的值,参数3表示满足条件的列名称,参数4为该列名下的值
            db.update("user1", values, "id=?", new String[]{"1"});
        }        
    }
    
    public class QueryOnClickListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
            SQLiteDatabase db = database_helper.getWritableDatabase();
            //查询的语法,参数1为表名;参数2为表中的列名;参数3为要查询的列名;参数时为对应列的值;该函数返回的是一个游标
            Cursor cursor = db.query("user1", new String[]{"id", "name"}, "id=?", new String[]{"1"},  null, null, null);
            //遍历每一个记录
            while(cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));//返回列名为name的值
                System.out.println("query---->" + name);
            }
        }       
    }
    
    public class DeleteOnClickListener implements OnClickListener{

        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
            SQLiteDatabase db = database_helper.getWritableDatabase();
            //直接删除名为tornadomeet对应的那条记录
            db.delete("user1", "name=?" ,new String[]{"tornadomeet"});
        }
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

2.DatabaseHelper.java

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

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int  VERSON = 1;//默认的数据库版本
    
    //继承SQLiteOpenHelper类的类必须有自己的构造函数
    //该构造函数4个参数,直接调用父类的构造函数。其中第一个参数为该类本身;第二个参数为数据库的名字;
    //第3个参数是用来设置游标对象的,这里一般设置为null;参数四是数据库的版本号。
    public DatabaseHelper(Context context, String name, CursorFactory factory, int verson){
        super(context, name, factory, verson);
    }
    
    //该构造函数有3个参数,因为它把上面函数的第3个参数固定为null了
    public DatabaseHelper(Context context, String name, int verson){
        this(context, name, null, verson);
    }
    
    //该构造函数只有2个参数,在上面函数 的基础山将版本号固定了
    public DatabaseHelper(Context context, String name){
        this(context, name, VERSON);
    }
    
    //该函数在数据库第一次被建立时调用
    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub
        System.out.println("create a sqlite database");
        //execSQL()为执行参数里面的SQL语句,因此参数中的语句需要符合SQL语法,这里是创建一个表
        arg0.execSQL("create table user1(id int, name varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub
        System.out.println("update a sqlite database");
    }

}

3.activity_main.xml

技术分享
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <Button 
 7         android:id="@+id/create_database"
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:layout_alignParentBottom="true"
11         android:text="@string/create_database"
12         />
13 
14     <Button
15         android:id="@+id/update_database"
16         android:layout_width="fill_parent"
17         android:layout_height="wrap_content"
18         android:layout_above="@id/create_database"
19         android:layout_alignParentLeft="true"
20         android:text="@string/update_database" />
21 
22     <Button
23         android:id="@+id/insert"
24         android:layout_width="fill_parent"
25         android:layout_height="wrap_content"
26         android:layout_above="@id/update_database"
27         android:layout_alignParentLeft="true"
28         android:text="@string/insert" />
29 
30     <Button
31         android:id="@+id/update"
32         android:layout_width="fill_parent"
33         android:layout_height="wrap_content"
34         android:layout_above="@id/insert"
35         android:layout_alignParentLeft="true"
36         android:text="@string/update" />
37     
38     <Button 
39         android:id="@+id/query"
40         android:layout_width="fill_parent"
41         android:layout_height="wrap_content"
42         android:layout_alignParentLeft="true"
43         android:layout_above="@id/update"
44         android:text="@string/query"
45         />
46     <Button 
47         android:id="@+id/delete"
48         android:layout_width="fill_parent"
49         android:layout_height="wrap_content"
50         android:layout_alignParentLeft="true"
51         android:layout_above="@id/query"
52         android:text="@string/delete"
53         />
54         
55 </RelativeLayout>
布局文件

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。