Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android的SQLite基本操作

Android的SQLite基本操作

編輯:關於Android編程

涉及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;
		}
	}
}


    

運行結果

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083870.png

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083833.png

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083850.png

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083867.png

 

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083832.png

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083937.png

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083959.png

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083946.png

data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316083987.png

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved