【达内课程】SQLite(一)创建数据库、创建数据表、增删改查
创建一个数据库
MainActivity
public class MainActivity extends AppCompatActivity {
private LinearLayout mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建数据库
createDatabase();
}
private void createDatabase() {
String name = "test.db";
int mode = MODE_PRIVATE;
SQLiteDatabase.CursorFactory cursorFactory = null;
openOrCreateDatabase(name,mode,cursorFactory);
}
}
运行程序,然后打开DDMS
找到data->data->包名->databases会看到自己创建的数据库test.db
test.db-journal是程序运行过程中自动创建的,可以忽略
创建数据表
MainActivity
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建数据库
createDatabase();
//创建数据表
createTable();
}
private void createTable() {
String sql = "CREATE TABLE users ("+
"name VARCHAR(10) UNIQUE NOT NULL, "+
"age INTEGER, "+
"phone CHAR(11) UNIQUE, "+
"email VARCHAR(32) UNIQUE"+
")";
db.execSQL(sql);
}
private void createDatabase() {
String name = "test.db";
int mode = MODE_PRIVATE;
SQLiteDatabase.CursorFactory cursorFactory = null;
db = openOrCreateDatabase(name,mode,cursorFactory);
}
}
运行程序后,同样打开DDMS,导出test.db
我们可以用Sqlite Expert这个工具查看
用法很简单,直接把test.db拖拽进来即可
左侧列出数据库和表,选中表,点击右侧Design可查看表结构
值得注意的是,我们创建数据库的时候写的是openOrCreateDatabase,打开或创建数据库,如果不存在该数据库就创建,如果存在就打开
而创建表是CREATE TABLE,会一直创建,所以如果再执行一遍程序,会崩溃,报表已存在的错:table users already exists
FATAL EXCEPTION: main
Process: com.example.a00xiaoyugmailcom.myapplication, PID: 17774
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a00xiaoyugmailcom.myapplication/com.example.a00xiaoyugmailcom.myapplication.MainActivity}: android.database.sqlite.SQLiteException: table users already exists (code 1): ……
我们先把createTable()注释掉
//createTable();
如果是字符串,需要用单引号标记,不能使用双引号
现在增加一条数据
MainActivity
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建数据库
createDatabase();
//创建数据表
//createTable();
//增加数据
insertData();
}
private void insertData() {
String sql = "INSERT INTO users "+
"(name,age,phone,email) "+
"values "+
"('Lily',40,'13333333333','aaa@qq.com')";
db.execSQL(sql);
}
private void createTable() {
String sql = "CREATE TABLE users ("+
"name VARCHAR(10) UNIQUE NOT NULL, "+
"age INTEGER, "+
"phone CHAR(11) UNIQUE, "+
"email VARCHAR(32) UNIQUE"+
")";
db.execSQL(sql);
}
private void createDatabase() {
String name = "test.db";
int mode = MODE_PRIVATE;
SQLiteDatabase.CursorFactory cursorFactory = null;
db = openOrCreateDatabase(name,mode,cursorFactory);
}
}
报错一
如果我们故意把字段name写错成nama会报错提示没有相应字段
table users has no column named nama (code 1):
其实SQL语句不区分大小写,只要拼写正确即可
执行程序,再次导出test.db,用刚才的工具打开,点击右侧Data查看数据
报错二
我们已经成功插入了一条数据,由于刚才创建约束的时候名字是unique的,所以如果我们再运行程序,再插入相同数据,
会提示你UNIQUE constraint failed: users.email,说明 users.email本应是唯一的,现在却重复了
FATAL EXCEPTION: main
Process: com.example.a00xiaoyugmailcom.myapplication, PID: 20592
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a00xiaoyugmailcom.myapplication/com.example.a00xiaoyugmailcom.myapplication.MainActivity}: android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: users.email (code 2067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
修改SQL语句,多增加几条数据
private void insertData() {
/*String sql = "INSERT INTO users "+
"(name,age,phone,email) "+
"values "+
"('Lily',40,'13333333333','aaa@qq.com')";*/
String sql = "INSERT INTO users "+
"(name,age,phone,email) "+
"values "+
"('Tony',27,'18716000000','aaa@qq.com')";
db.execSQL(sql);
}
下面来测试下删除功能
现在的数据
删除名字是”Tom”的数据
MainActivity
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建数据库
createDatabase();
//创建数据表
//createTable();
//增加数据
//insertData();
//删除数据
deleteData();
}
private void deleteData() {
String sql = "DELETE FROM users WHERE name = 'Tom'";
db.execSQL(sql);
Log.d("SQL","删除成功");
}
......
}
修改sql语句,删除年龄大于26岁的数据
String sql = "DELETE FROM users WHERE age > 27";
现在就只剩Tony的数据了
修改sql语句,删除所有数据,会删除users表中所有数据
String sql = "DELETE FROM users";
下面测试修改功能
原始数据
private void updateData() {
String sql = "UPDATE users SET age = 22 WHERE name = 'Lily'";
db.execSQL(sql);
}
修改后的数据
上一篇: 自建博客(day4之配置shiro)
下一篇: python中的SQLite操作