編輯:關於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; } } }
運行結果
本文實例講述了Android和JavaScript相互調用的方法。分享給大家供大家參考,具體如下:Html頁面和Java代碼結合的方式一般用在界面經常被更改 的情況下,可
Android中的Intent是一個非常重要且常用的類,可以用來在一個組件中啟動App中的另一個組件或者是啟動另一個App的組件,這裡所說的組件指的是Activity、S
handler在安卓開發中是必須掌握的技術,但是很多人都是停留在使用階段。使用起來很簡單,就兩個步驟,在主線程重寫handler的handleMessage( )方法,在
通過上一節的分析,我們發現InputDispatcherThread使用InputChannel的sendMessage方法發送了一條消息,但是我們不知道誰在接收這條消息