问题描述
- 新人Android ADT的问题
-
我根据教程写的SQLite数据库测试,可以
创建表,但一直不执行插入数据这个方法,代码如下package com.example.xiangwei.TestCase;
import com.example.xiangwei.MyOpenHelper;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;public class TestCase extends AndroidTestCase {
private MyOpenHelper oh;
private SQLiteDatabase db;
public void test(){
//getContext():获取一个虚拟的上下文
MyOpenHelper oh = new MyOpenHelper(getContext(), "xiangwei.db", null, 1);
SQLiteDatabase db = oh.getWritableDatabase();
}
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
oh = new MyOpenHelper(getContext(), "xiangwei.db", null, 1);
db = oh.getWritableDatabase();
System.out.println("执行了setup");
}public void insert(){
db.execSQL("insert into person (name,salary,phone)values(?,?,?)",new Object[]{"向伟",13000,"132976"});
db.execSQL("insert into person (name, salary, phone)values(?, ?, ?)", new Object[]{"小志", 14000, "13888"});
System.out.println("向伟");
}
public void insertApi(){
//把要插入的数据全部封装至ContentValues对象
ContentValues values = new ContentValues();
values.put("name", "游天龙");
values.put("phone", "15999");
values.put("salary", 16000);
db.insert("person", null, values);
System.out.println("向伟");
}
}
解决方案
关闭操作做了没???
解决方案二:
做了,可以在数据库中建立表,就是执行不了insert方法