sqlite

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
        <Button
        android:id="@+id/create"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="创建数据库"
        />
        <Button
        android:id="@+id/insert"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="插入数据库"
        />
        <Button
        android:id="@+id/update"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="更新数据库"
        />
        <Button
        android:id="@+id/query"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="查询数据库"
        />
        <Button
        android:id="@+id/delete"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="删除数据库"
        />
</LinearLayout>

  

package com.example.sqlite;
  
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
   
public class MainActivity extends Activity implements OnClickListener{

    private Button create;
    private Button insert;
    private Button query;
    private Button update;
    private Button delete;
    private MyDBhelper dbHelper;
    SQLiteDatabase db;
    Cursor cursor;
    public static int counter=0;
    public String name,sex;
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    
    private void initView(){
        create=(Button) findViewById(R.id.create);
        insert=(Button) findViewById(R.id.insert);
        query=(Button) findViewById(R.id.query);
        update=(Button) findViewById(R.id.update);
        delete=(Button) findViewById(R.id.delete);
        create.setOnClickListener(this);
        insert.setOnClickListener(this);
        query.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);
    }
    
    private void createDB(){
        dbHelper=new MyDBhelper(this);
        db=dbHelper.getWritableDatabase();
        Toast.makeText(this, "创建成功!", Toast.LENGTH_SHORT).show();
    }
    
    private void insertDB(){
        counter++;
        name="用户"+counter;
        if(counter%2==0){
            sex="male";
        }else{
            sex="female";
        }
        dbHelper=new MyDBhelper(this);
        db=dbHelper.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("name",name);
        cv.put("age",26);
        cv.put("sex",sex);
        db.insert(MyDBhelper.TABLE_NAME, null, cv);
        Toast.makeText(this, name+"添加成功!", Toast.LENGTH_SHORT).show();
        db.close();
    }
    
    private void queryDB(){
        dbHelper=new MyDBhelper(this);
        SQLiteDatabase rdb=dbHelper.getReadableDatabase();
        cursor=rdb.query(MyDBhelper.TABLE_NAME, null, null, null, null, null, null);
        StringBuffer sb=new StringBuffer(); 
        while(cursor.moveToNext()){
            String rs=cursor.getInt(cursor.getColumnIndex(MyDBhelper.ID))+","+cursor.getString(cursor.getColumnIndex(MyDBhelper.NAME))+","+cursor.getInt(cursor.getColumnIndex(MyDBhelper.AGE))+","+cursor.getString(cursor.getColumnIndex(MyDBhelper.SEX))+"\n";
            sb.append(rs);
        }   
        Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
        db.close();
    }
    
    private void deleteDB(){
        dbHelper=new MyDBhelper(this);
        db=dbHelper.getWritableDatabase();
        String whereClause="_id>?";
        String[] whereArgs={String.valueOf(1)};
        db.delete(MyDBhelper.TABLE_NAME, whereClause, whereArgs);
        db.close();
        Toast.makeText(this, "删除成功!", Toast.LENGTH_SHORT).show();
    }
    
    private void updateDB(){
        dbHelper=new MyDBhelper(this);
        db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("age", 21);
        String whereClause="name=?";
        String[] whereArgs={String.valueOf("小强")};
        db.update(MyDBhelper.TABLE_NAME, values, whereClause, whereArgs);
        db.close();
        Toast.makeText(this, "更新成功!", Toast.LENGTH_SHORT).show();
    }
    
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.create:
            createDB();
            break;
       case R.id.insert:
           insertDB();
            break;
       case R.id.query:
            queryDB();
            break;
       case R.id.update:
           updateDB();
            break;
       case R.id.delete:
           deleteDB();
            break;
        }
    } 
}

  

package com.example.sqlite;
  
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
   
public class MyDBhelper extends SQLiteOpenHelper{

    public static final String ID="_id";
    public static final String NAME="name";
    public static final String AGE="age";
    public static final String SEX="sex";
    public static final String TABLE_NAME="person";  

    public MyDBhelper(Context context) {
        super(context,"persons", null,1);
    }
    
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+TABLE_NAME+"("+
                ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"
                +NAME+" TEXT NOT NULL,"
                +AGE+" INTEGER,"
                +SEX+" TEXT"+")");
        System.out.println("database created!");
    }
    
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        System.out.println("database update!");
    }
}

  

    原文作者:sqlite
    原文地址: https://www.cnblogs.com/cuthead/p/sqlite.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞