Activity开发之Sqlite

package com.sqlite;

import com.sqlite.db.DatabaseHelper;

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 SQLiteActivity extends Activity {
    /** Called when the activity is first created. */
	
	private static int id=1;
	
	private Button createDatabase;
	private Button upgrade;
	private Button insert;
	private Button update;
	private Button query;
	private Button delete;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        createDatabase = (Button) findViewById(R.id.createDatabase);
        upgrade = (Button) findViewById(R.id.upgradeDatabase);
        insert = (Button) findViewById(R.id.insert);
        update = (Button) findViewById(R.id.update);
        query = (Button) findViewById(R.id.query);
        delete = (Button) findViewById(R.id.delete);
        
        //创建数据库
        createDatabase.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this, "test_wepull");
				//获得一个可写的对象
				dbHelper.getWritableDatabase();
			}
		});
        insert.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this, "test_wepull");
				//获得一个可写的对象
				SQLiteDatabase db = dbHelper.getWritableDatabase();
				
				//定义ContentValues对象
				ContentValues values = new ContentValues();
				//第一个参数是所要插入的哪个字段,第二个参数就是所要插入的值
				values.put("id", id++);
				values.put("name", "zhangsan"+id);
				db.insert("user", null, values);
			}
		});
        query.setOnClickListener(new OnClickListener() {
			
        	//第一个参数,所要查询的表名
        	//第二个参数,所要查询的表中字段
        	//第三个参数,相当于where id=?
        	//第四个参数,里面的值赋给第三个参数占位符
        	//后面条件参数可有可无
			@Override
			public void onClick(View v) {
				DatabaseHelper dbHelper = new DatabaseHelper(SQLiteActivity.this, "test_wepull");
				//获得一个可写的对象
				SQLiteDatabase db = dbHelper.getReadableDatabase();
				Cursor cursor = db.query("user", new String[]{"id","name"}, "id>=?", new String[]{"1"}, null, null, null);
				
				//每循环一次,游标向下移动,直到没有数据为止
				while(cursor.moveToNext()){
					int id = cursor.getInt(cursor.getColumnIndex("id"));
					String name = cursor.getString(cursor.getColumnIndex("name"));
					System.out.println(id+"--->"+name);
				}
			}
		});
    }
}
package com.sqlite.db;

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

/**
 * 	DatabaseHelper类有两个作用
 * 	1.提供了getReaderableDatabase()和getWriteableDatabase()供我们操作数据库的读写
 * 	2.提供了onCreate()和upgrade()方法,创建数据库或者修改
 * @author Administrator
 *
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	
	
	private static final int VERSION=1;
	//必须有的构造方法
	//第一个参数,context是一个操作数据库的Activity对象
	//第二个参数,表名
	//第三个参数,设置为null即可
	//第四个参数,数据库的版本信息
	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		//必须通过super关键字来讲父类的构造方法继承
		super(context, name, factory, VERSION);
		
	}
	public DatabaseHelper(Context context, String name,int version){
		this(context, name, null, version);
	}
	public DatabaseHelper(Context context, String name){
		this(context, name,VERSION);
	}

	//onCreate()方法在数据库第一次创建的时候调用,或者说在获取数据库对象的时候
	@Override
	public void onCreate(SQLiteDatabase db) {
		System.out.println("create a database");
		
		db.execSQL("create table user(id int,name varchar(20))");
	}

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

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button
	android:id="@+id/createDatabase"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="createDatabase"
	/>
<Button
	android:id="@+id/upgradeDatabase"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="upgradeDatabase"
	/>
<Button
	android:id="@+id/insert"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="insert"
	/>
<Button
	android:id="@+id/update"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="update"
	/>
<Button
	android:id="@+id/query"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="query"
	/>
<Button
	android:id="@+id/delete"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="delete"
	/>
</LinearLayout>


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值