Android中的SQLite
1.SQL语句的插入数据
package com.example.sqlitedemo1;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 每个程序都有自己的数据库 默认情况下是各自互相不干扰
// 创建一个数据库 并且打开
SQLiteDatabase db = openOrCreateDatabase("user.db", MODE_PRIVATE, null); //定义数据库的名字和让数据库仅本程序访问(Private)
db.execSQL("create table if not exists usertb (_id integer primary key autoincrement, name text not null , age integer not null , sex text not null )"); //上面下划线_id比较重要
db.execSQL("insert into usertb(name,sex,age) values('张三','女',18)");
db.execSQL("insert into usertb(name,sex,age) values('李四','女',19)");
db.execSQL("insert into usertb(name,sex,age) values('王五','男',20)");
Cursor c = db.rawQuery("select * from usertb", null);//查询
if (c != null) {
while (c.moveToNext()) {
Log.i("info", "_id:" + c.getInt(c.getColumnIndex("_id")));
Log.i("info", "name:" + c.getString(c.getColumnIndex("name")));
Log.i("info"<