android-----sqllite小型数据库

1、main.xml代码:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SQLLiteAcivity" >

	<Button
	    android:id="@+id/button01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/createD" />
	<Button
	    android:id="@+id/button02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/updateD" 
        android:layout_below="@id/button01"/>
	<Button
	    android:id="@+id/button03"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/insert" 
        android:layout_below="@id/button02"/>
	<Button
	    android:id="@+id/button04"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/update" 
        android:layout_below="@id/button03"/>
	<Button
	    android:id="@+id/button05"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/query"
        android:layout_below="@id/button04" />
</RelativeLayout>

2、string.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">sqllite</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
	<string name="createD">createDatabase</string>
	<string name="updateD">updateDatabase</string>
	<string name="insert">insert</string>
	<string name="update">update</string>
	<string name="query">query</string>
	
</resources>

3、com.example.sqllite包里SQLLiteActivty.java代码:

package com.example.sqllite;

import com.example.sqllite.db.DatabaseHelper;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;

public class SQLLiteAcivity extends Activity {
	private Button button01=null;
	private Button button02=null;
	private Button button03=null;
	private Button button04=null;
	private Button button05=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		button01=(Button)findViewById(R.id.button01);
		button02=(Button)findViewById(R.id.button02);//更新数据库版本
		button03=(Button)findViewById(R.id.button03);
		button04=(Button)findViewById(R.id.button04);//更新数据库
		button05=(Button)findViewById(R.id.button05);
		
		button01.setOnClickListener( new createListener());
		button02.setOnClickListener( new updateDListener());
		button03.setOnClickListener( new insertListener());
		button04.setOnClickListener( new updateListener());
		button05.setOnClickListener( new queryListener());
	}
	class createListener implements OnClickListener,android.view.View.OnClickListener{

		public void onClick(DialogInterface dialog, int which) {
			DatabaseHelper dbHelper=new DatabaseHelper(SQLLiteAcivity.this,"sqllite_db");
			SQLiteDatabase db=dbHelper.getReadableDatabase();
		}
		public void onClick(View v) {}
	}
	
	class updateDListener implements android.view.View.OnClickListener{
		@Override
		public void onClick(View v) {
			//创建一个DatabaseHelper对象
			DatabaseHelper dbHelper=new DatabaseHelper(SQLLiteAcivity.this,"sqllite_db",2);
			//只有调用了DatabaseHelper对象的getReadableDatabase()和getWritableDatabase()方法,才能创建数据库
			SQLiteDatabase db=dbHelper.getReadableDatabase();	
		}
	}
	
	class insertListener implements android.view.View.OnClickListener{
		public void onClick(View v) {
			ContentValues values=new ContentValues();
			values.put("id",1);
			values.put("name", "zhangsan");
			DatabaseHelper dbHelper=new DatabaseHelper(SQLLiteAcivity.this,"sqllite_db");
			SQLiteDatabase db=dbHelper.getReadableDatabase();
			db.insert("user", null, values);
		}
	}
	
	class updateListener implements android.view.View.OnClickListener{

		public void onClick(View v) {
			DatabaseHelper dbHelper=new DatabaseHelper(SQLLiteAcivity.this,"sqllite_db");
			SQLiteDatabase db=dbHelper.getReadableDatabase();
			ContentValues values=new ContentValues();
			values.put("name", "zhangsanfeng");
			//(表名;一个ContentValues对象;where子句;前面有几个占位符,后面就有几个值)。
			db.update("user", values,"id=?",new String[]{"1"});
		}
	}
	
	class queryListener implements android.view.View.OnClickListener{
		public void onClick(View v) {
			DatabaseHelper dbHelper=new DatabaseHelper(SQLLiteAcivity.this,"sqllite_db");
			SQLiteDatabase db=dbHelper.getReadableDatabase();
			Cursor cursor=db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);
			while(cursor.moveToNext()){
				String name=cursor.getString(cursor.getColumnIndex("name"));
				System.out.println("query---->"+name);
			}
		}
	}
}

4、com.example.sqllite.db包里DatabaseHelper.java代码:

package com.example.sqllite.db;

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 VERSION=1;
	
	public DatabaseHelper(Context context, String name, CursorFactory factory,int version) {
		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);
	}
	
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table user(id int,name varchar(20))");
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}

5、运行效果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值