使用sqliteopenhelper在android上创建数据库时出现问题

时间:2014-07-14 17:42:17

标签: android database eclipse sqlite

我必须使用sqlopenhelper创建表。当我在模拟器中运行应用程序时,代码没有给出错误,但是正在显示来自表创建的日志消息。当我尝试注册应用程序时停止。

我使用了以下代码:

package com.example.catchthebus;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbHelp extends SQLiteOpenHelper {

    SQLiteDatabase db;
    public static String DB="CatchTheBus";


    public static final String Table_name="Driver";
    public static final String Driver_ID="Driver_ID";
    public static final String Driver_Name="Driver_Name";
    public static final String Driver_Contact ="Driver_Contact";
    public static final String Route_Num ="Route_Num";


    public static final String Table_Create="create table" +Table_name+"("+Driver_ID +"integer PRIMARY KEY,"+Driver_Name+" varchar(25),"+Driver_Contact +"integer,"+Route_Num +"integer"+");";


    public static final String LOGTAG = null;   




    public DbHelp(Context context) {
        super(context, DB, null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try
        {
        db.execSQL(Table_Create);
        }
        catch(SQLException e)
        {
             e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

public void addDriver(String name ,String contact,String Route_num) {

        db.execSQL("insert into Drivers values(null, '"+name+"' ,"+contact+" , "+ Route_num+" )");
        String tag = null;
        Log.i(tag, "inserted value");


    }
}





main activity code:

package com.example.catchthebus;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    //SqlHelp sql_helper;
    //SqlHelp sql_helper1;
    //SqlHelp sql2;

    SQLiteOpenHelper dbhelp;
    SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbhelp = new DbHelp(this);
        database= dbhelp.getWritableDatabase();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;


    }

    public void Open_Passenger_Submit(View view)
    {
        Intent intt=new Intent(this,Submit_Passenger.class);
        startActivity(intt);

        //sql_helper.createPassenger();
    }
    public void Open_Driver_Submit(View view)
    {`enter code here`
        Intent intt=new Intent(this,Submit_Driver.class);
        startActivity(intt);
        //sql_helper.createDriver();


    }
}

1 个答案:

答案 0 :(得分:0)

看起来你的Table_Create字符串缺少一些空格。

public static final String Table_Create =“create table”+ Table_name +“(”+ Driver_ID +“integer PRIMARY KEY,”+ Driver_Name +“TEXT,”+ Driver_Contact +“integer,”+ Route_Num +“integer);”; < / p>