SqlLite

本文提供了一个Android应用中使用SQLite数据库进行增删改查操作的具体示例。包括创建表、插入记录、删除记录、更新记录和查询记录等核心功能。
package com.example.day_02_sqlite;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class MainActivity extends Activity {


private SQLiteDatabase db;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase("db", MODE_PRIVATE, null);
// db.insert(table, nullColumnHack, values)
// db.delete(table, whereClause, whereArgs)
// db.update(table, values, whereClause, whereArgs)
// db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)
db.execSQL("create table student(id integer primary key autoincrement,name varchar(20),sex varchar(20))");
}


// 插入
public void insert(View v) {
db.execSQL("insert into student (name,sex) values (?,?)",new String[]{"张三","男"});
db.execSQL("insert into student (name,sex) values (?,?)",new String[]{"李四","女"});
db.execSQL("insert into student (name,sex) values ('王五','妖')"/*,new String[]{"王五","妖"}*/);
}


// 删
public void delete(View v) {

db.execSQL("delete from student where name = ?",new String[]{"王五"});

}


// 改
public void update(View v) {

db.execSQL("update student set name = ? where name = ?",new String[]{"张二","张三"});

}


// 查
public void select(View v) {

Cursor cursor = db.rawQuery("select * from student where sex = ?",new String[]{"妖"});

// cursor.
//是否可以向下移动,如果可以移动就代表有数据
while (cursor.moveToNext()) {
//先得到name列的角标,再通过角标获取name列的数据
String name =cursor.getString(cursor.getColumnIndex("name"));
String sex =cursor.getString(cursor.getColumnIndex("sex"));

Log.i("MainActivity", "name =" + name +"  sex="+sex);

}

}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值