package com.example.sqlitedemo2;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase db=openOrCreateDatabase("stu.db",MODE_PRIVATE,null);
db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name text not null,sex text not null,age integer not null)");
ContentValues values=new ContentValues();
values.put("name","张三");
values.put("sex","男");
values.put("age",18);
db.insert("stutb",null, values);
values.clear();
values.put("name","李四");
values.put("sex","男");
values.put("age",18);
db.insert("stutb",null, values);
values.clear();
values.put("name","小丽");
values.put("sex","女");
values.put("age",18);
db.insert("stutb",null, values);
values.clear();
values.put("sex","女");
db.update("stutb", values,"sex=?",new String[]{"男"});
db.delete("stutb","name like ?",new String[]{"%三%"});
Cursor cursor=db.query("stutb",null,"name=?",new String[]{"小丽"}, null,null,"name");
if(cursor!=null){
while(cursor.moveToNext()){
Log.i("打印","姓名:"+cursor.getString(1));
}
cursor.close();
}
db.close();
}
}
使用内置函数操作数据库
最新推荐文章于 2024-01-17 08:00:00 发布