package org.prothro; import android.app.Activity; 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; import android.widget.EditText; import android.widget.Toast; public class SQLiteTestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/db.db3", null); Button btn = (Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { final EditText username = (EditText)findViewById(R.id.username); final EditText password = (EditText)findViewById(R.id.password); @Override public void onClick(View v) { // TODO Auto-generated method stub try{ db.execSQL("insert into user values('"+username.getText().toString()+"','"+password.getText().toString()+"')"); }catch(SQLException e){ e.printStackTrace(); db.execSQL( "create table user(username varchar(20),password varchar(20));" ); db.execSQL("insert into user values('"+username+"','"+password+"')"); } //取出数据库中所有的数据 Cursor cursor = db.rawQuery("select * from user", null); while(cursor.moveToNext()){ Toast.makeText( SQLiteTestActivity.this, cursor.getString(cursor.getColumnIndex("username"))+" "+cursor.getString(cursor.getColumnIndex("password")), 1000).show(); } } }); } }