package com.xiangqiao.sqlite3;

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;

public class MainActivity extends Activity {
    
	Button createDatabase=null;
	Button upgradeDatabase=null;
	Button insert=null;
	Button delete=null;
	Button update=null;
	Button query=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        createDatabase=(Button)findViewById(R.id.create);
        createDatabase.setOnClickListener(listener);
        upgradeDatabase=(Button)findViewById(R.id.updatabse);
        upgradeDatabase.setOnClickListener(listener);
        insert=(Button)findViewById(R.id.insert);
        insert.setOnClickListener(listener);
         delete=(Button)findViewById(R.id.delete);
         delete.setOnClickListener(listener);
        update=(Button)findViewById(R.id.update);
        update.setOnClickListener(listener);
        query=(Button)findViewById(R.id.query);
        query.setOnClickListener(listener);
    }
    
    private OnClickListener listener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			Button button=(Button)v;
		   //创建数据库
			if(v.getId()==R.id.create){
				 SQLiteHelper sqLiteHelper=new SQLiteHelper(MainActivity.this,"xiangqiao");
				 sqLiteHelper.getWritableDatabase();
			}
			 //升级数据库
			if(v.getId()==R.id.updatabse){
				System.out.println("---------------");;
				SQLiteHelper sqLiteHelper=new SQLiteHelper(MainActivity.this,"xiangqiao",2);
				 sqLiteHelper.getWritableDatabase();
			}
			 //增
			if(v.getId()==R.id.insert){
				SQLiteHelper sqLiteHelper=new SQLiteHelper(MainActivity.this,"xiangqiao");
				 SQLiteDatabase sqLiteDatabase=sqLiteHelper.getWritableDatabase();
				 
				 ContentValues values=new ContentValues();
				values.put("id", 1);
	            values.put("name", "xiangqiao");
				 
				 sqLiteDatabase.insert("user", null, values);
			}
			 //删
			if(v.getId()==R.id.delete){
				SQLiteHelper sqLiteHelper=new SQLiteHelper(MainActivity.this,"xiangqiao");
				 SQLiteDatabase sqLiteDatabase=sqLiteHelper.getWritableDatabase();
				 
				 sqLiteDatabase.delete("user", "id=?", new String[]{"1"});
			}
			 //改
			if(v.getId()==R.id.update){
				SQLiteHelper sqLiteHelper=new SQLiteHelper(MainActivity.this,"xiangqiao");
				 SQLiteDatabase sqLiteDatabase=sqLiteHelper.getWritableDatabase();
				 
				 ContentValues values=new ContentValues();
		            values.put("name", "xiangqiaoqiao");
				 sqLiteDatabase.update("user", values, "id=?", new String[]{"1"});
			}
			 //查
			if(v.getId()==R.id.query){
				SQLiteHelper sqLiteHelper=new SQLiteHelper(MainActivity.this,"xiangqiao");
				 SQLiteDatabase sqLiteDatabase=sqLiteHelper.getWritableDatabase();
				 
				Cursor cursor=sqLiteDatabase.query("user", new String []{"id","name"}, "id=?", new String []{"1"}, "", "", "");
				while(cursor.moveToNext()){
					String name=cursor.getString(cursor.getColumnIndex("name"));
					System.out.println("------->"+name);
				}
			}
			
		}
	};
}


 

 

 

package com.xiangqiao.sqlite3;

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

public class SQLiteHelper extends SQLiteOpenHelper {

	private static int version=1; 
	public SQLiteHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	public SQLiteHelper(Context context, String name) {
		this(context, name, version);
	}
	public SQLiteHelper(Context context, String name,
			int version) {
		 this(context, name, null, version);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table user (id int,name string)");
        System.out.println("create  database table");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		 System.out.println("upgrade  database ");
	}

}


 

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐