选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
String n=name.getText().toString();
String e=email.getText().toString();
//String add="insert into lgx_table(name,email) values('lgx','xxx')";
String add="insert into lgx_table(name,email) values(?,?)";
db.execSQL(add,new String[]{n,e});
android简单的数据库创建和使用
package com.example.dbdemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBd extends SQLiteOpenHelper {
public DataBd(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table lgx_table(_id integer primary key autoincrement,name varchar(20), email varchar(50))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.example.dbdemo;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
DataBd dataBd;
SQLiteDatabase db;
EditText name,email;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dataBd = new DataBd(this, "lgx", null, 2);
db = dataBd.getReadableDatabase();
name=(EditText) findViewById(R.id.name);
email=(EditText) findViewById(R.id.email);
text=(TextView) findViewById(R.id.text);
}
public void dbClick(View v) {
switch (v.getId()) {
case R.id.add:
String n=name.getText().toString();
String e=email.getText().toString();
//String add="insert into lgx_table(name,email) values('lgx','xxx')";
String add="insert into lgx_table(name,email) values(?,?)";
db.execSQL(add,new String[]{n,e});
break;
case R.id.delete:
break;
case R.id.update:
break;
case R.id.select:
String select="select * from lgx_table ";
//db.execSQL(select);
Cursor cursor=db.rawQuery(select, null);
while (cursor.moveToNext()) {
String name=cursor.getString(cursor.getColumnIndex("name"));
String email=cursor.getString(cursor.getColumnIndex("email"));
text.setText(name+" "+email);
}
break;
default:
break;
}
}
}