数据库/表未创建

时间:2014-08-05 07:05:03

标签: android eclipse android-sqlite

没有创建SQLite数据库或表。

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "employee_directory1";

    protected Context context;
    private static final int DATABASE_VERSION = 1;
    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String s;
        try {
            Toast.makeText(context, "1",Toast.LENGTH_SHORT).show();
            InputStream in = context.getResources().openRawResource(R.raw.sql);
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document doc = builder.parse(in, null);
            NodeList statements = doc.getElementsByTagName("statement");
            for (int i=0; i<statements.getLength(); i++) {
                s = statements.item(i).getChildNodes().item(0).getNodeValue();
                db.execSQL(s);
            }
        } catch (Throwable t) {
            Toast.makeText(context, t.toString(), 50000).show();
        }
    }

sql.xml文件

<sql>
<statement>
CREATE TABLE IF NOT EXISTS employee (
    _id INTEGER PRIMARY KEY AUTOINCREMENT, 
    firstNAme VARCHAR(500), 
    lastNAme VARCHAR(500))

</statement>

创建时的另一个文件

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.main);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

        setContentView(R.layout.main);
        searchText = (EditText) findViewById(R.id.searchText);
        db = (new DatabaseHelper(this)).getWritableDatabase();
    }

1 个答案:

答案 0 :(得分:0)

代码中的

sql查询执行部分 请更改oncreate的{​​{1}}中的代码。我成功创建了表

DatabaseHelper