码迷,mamicode.com
首页 > 移动开发 > 详细

Android的SQLite基本操作

时间:2015-01-24 22:53:02      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:

 涉及SQLite的增删改查,结果用log显示

package com.example.sqlconnecttest;

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

public class DBHelper extends SQLiteOpenHelper{

	/*
	 * 必须有的构造器
	 */
	public DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	/*
	 * 当第一次创建数据库时,就调用该方法
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i("createDatabases", "创建数据库--->");
	}

	/*
	 * 当更新数据库时,调用该方法
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.i("updateDatabase", "更新数据库--->");
	}
}

 

package com.example.sqlconnecttest;

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

public class MainActivity extends Activity implements OnClickListener{

	private Button createDatabase;
	private Button createTable;
	private Button insert;
	private Button select;
	private Button update;
	private Button delete;
	
	private final String DATABASE_NAME = "myDatabase";
	private SQLiteDatabase mySQLiteDatabase = null;
	private final String TABLE_NAME = "user";
	private String SQL = 
			"CREATE TABLE [user] ("+
            "[id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "+
            "[username] VARCHAR NOT NULL, "+
            "[password] VARCHAR NOT NULL, "+
            "[phoneNumber] VARCHAR NOT NULL)";
	private DBHelper db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		createView();
		setListener();
	}

	private void createView(){
		createDatabase = (Button) findViewById(R.id.createDatabase);
		createTable = (Button) findViewById(R.id.createTable);
		insert = (Button) findViewById(R.id.insert);
		select = (Button) findViewById(R.id.select);
		update = (Button) findViewById(R.id.update);
		delete = (Button) findViewById(R.id.delete);
	}
	//加监听器
	private void setListener(){
		createDatabase.setOnClickListener(this);
		createTable.setOnClickListener(this);
		insert.setOnClickListener(this);
		select.setOnClickListener(this);
		update.setOnClickListener(this);
		delete.setOnClickListener(this);
	}
        //各种监听方法
	@Override
	public void onClick(View view) {
		switch(view.getId()){
		case R.id.createDatabase:
		{
			db = new DBHelper(MainActivity.this,DATABASE_NAME,null,1);
			mySQLiteDatabase = db.getWritableDatabase();
			Log.i("数据库对象", mySQLiteDatabase.toString());
			//System.out.print("数据库对象"+mySQLiteDatabase);
			break;
		}
		case R.id.createTable:
		{
			db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
			mySQLiteDatabase = db.getWritableDatabase();
			try {
				mySQLiteDatabase.execSQL(SQL);
				Log.i("创建表", SQL);
			} catch (SQLException e) {
				e.printStackTrace();
				Log.i("创建表--》", "创建表失败----------------》");
			}
			break;
		}
		case R.id.insert:
		{
			db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
			mySQLiteDatabase = db.getWritableDatabase();
			ContentValues cv = new ContentValues();
			cv.put("username", "老爸");
			cv.put("password", 123456);
			cv.put("phoneNumber", "134756658888");
			long n = mySQLiteDatabase.insert(TABLE_NAME, null, cv);
			Log.i("插入数据", n + "");
			mySQLiteDatabase.close();
			break;
		}
		case R.id.select:
		{
			db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
			mySQLiteDatabase = db.getReadableDatabase();
			/**
			 * 参数1:表名
			 * 参数2:要显示的列
			 * 参数3:where语句
			 * 参数4:where语句的条件值
			 * 参数5:分组方式
			 * 参数6:having条件
			 * 参数7:排序方式
			 */
			Cursor cursor = mySQLiteDatabase.query(TABLE_NAME, 
					new String[]{"id","username","password","phoneNumber"}, 
					null, null, null, null, null);
			while(cursor.moveToNext()){
				int id = cursor.getInt(cursor.getColumnIndex("id"));
				String username = cursor.getString(cursor.getColumnIndex("username"));
				String password = cursor.getString(cursor.getColumnIndex("password"));
				String phoneNumber = cursor.getString(cursor.getColumnIndex("phoneNumber"));
				Log.i("query-->", "id:" + id + "userName:" + username + 
					  "password" + password + "phoneNumber" + phoneNumber);
			}
			mySQLiteDatabase.close();
			break;
		}
		case R.id.update:
		{
			db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
			mySQLiteDatabase = db.getWritableDatabase();
			ContentValues cv1 = new ContentValues();
			cv1.put("username", "admin");
			cv1.put("password", "admin");
			cv1.put("phoneNumber", "1388888");
			String whereClause = "id" + "=?";
			String[] whereArgs = {"8"};
			/*
			 * 参数1:表名
			 * 参数2:是一个ContextValue对象,就是更新的值
			 * 参数3:where语句条件
			 * 参数4:where条件的值
			 */
			int index = mySQLiteDatabase.update(TABLE_NAME, cv1, whereClause, whereArgs);
			Log.i("update-->", index + "");
			break;
		}
		case R.id.delete:
		{
			db = new DBHelper(MainActivity.this, DATABASE_NAME, null, 1);
			mySQLiteDatabase = db.getWritableDatabase();
			/***
			 * 参数1:表名
			 * 参数2:where语句字段 
			 * 参数3:where语句字段的值
			 */
			String whereClause1 = "id" + "=?";
			String[] whereArgs1 = {"1"};
			int num = mySQLiteDatabase.delete(TABLE_NAME, whereClause1, whereArgs1);
			Log.i("删除记录-->", num + "");
			mySQLiteDatabase.close();
			break;
		}
		default:
			break;
		}
	}
}
<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="com.example.sqlconnecttest.MainActivity" >

    <TextView 
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SQLite数据库"
        />
    <Button 
        android:id="@+id/createDatabase"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:text="创建一个数据库"
        />
    <Button 
        android:id="@+id/createTable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/createDatabase"
        android:text="创建一张表"
        />
    <Button 
        android:id="@+id/insert"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/createTable"
        android:text="插入数据"
        />
    <Button 
        android:id="@+id/select"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/insert"
        android:text="查询数据"
        />
    <Button 
        android:id="@+id/update"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/select"
        android:text="更新数据"
        />
    <Button 
        android:id="@+id/delete"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/update"
        android:text="删除数据"
        />

</RelativeLayout>

运行结果

技术分享

技术分享

技术分享

技术分享

 

技术分享

技术分享

技术分享

技术分享

技术分享

Android的SQLite基本操作

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/43090901

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!