遵循教程

时间:2017-12-13 22:21:11

标签: android sqlite android-sqlite

这是我创建的两个类“DatabaseHelper”包含我创建数据库和表的查询,我的主要活动名为“Home”,这就是我创建变量“myDB”并实例化调用在“DatabaseHelper”类中创建oncreate函数...但是当我尝试运行我的应用程序时它会崩溃!或者给我错误消息“无法绑定到本地8600 for debugger”plz help

DatabaseHelper:

package com.example.hp.teachme;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Teachme.db";
    public static final String TABLE_NAME = "prof_table.db";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "PRENOM";
    public static final String COL_3 = "NOM";
    public static final String COL_4 = "TELEPHONE";
    public static final String COL_5 = "ADRESSE";
    public static final String COL_6 = "MATIERE";
    public static final String COL_7 = "NIVEAU";


    public DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT, PRENOM TEXT, NOM TEXT, TELEPHONE TEXT, ADRESSE TEXT, MATIERE TEXT, NIVEAU TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){

        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);

    }
} 

主页:

package com.example.hp.teachme;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Home extends AppCompatActivity {
    DatabaseHelper myDb;

    Button prof,etudiant;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        myDb = new DatabaseHelper(this);


        prof=(Button)findViewById(R.id.homme_prof);
        etudiant=(Button)findViewById(R.id.homme_etudiant);


        prof.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(Home.this,login.class);
                startActivity(intent);
            }
        });

        etudiant.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent=new Intent(Home.this,recherche_map.class);
                startActivity(intent);
            }
        });

    }
}

Stacktrace:

               --------- beginning of crash
12-13 22:49:25.760 1634-1634/com.example.hp.teachme E/AndroidRuntime: FATAL 
EXCEPTION: main
                                                                  Process: com.example.hp.teachme, PID: 1634
                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hp.teachme/com.example.hp.teachme.Home}: android.database.sqlite.SQLiteException: unknown database prof_table (code 1): , while compiling: create table prof_table.db (ID INTEGER PRIMARY KEY AUTOINCREMENT, PRENOM TEXT, NOM TEXT, TELEPHONE TEXT, ADRESSE TEXT, MATIERE TEXT, NIVEAU TEXT)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:154)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:234)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                   Caused by: android.database.sqlite.SQLiteException: unknown database prof_table (code 1): , while compiling: create table prof_table.db (ID INTEGER PRIMARY KEY AUTOINCREMENT, PRENOM TEXT, NOM TEXT, TELEPHONE TEXT, ADRESSE TEXT, MATIERE TEXT, NIVEAU TEXT)
                                                                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
                                                                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
                                                                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                      at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
                                                                      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                      at com.example.hp.teachme.DatabaseHelper.onCreate(DatabaseHelper.java:27)
                                                                      at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                      at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                      at com.example.hp.teachme.DatabaseHelper.<init>(DatabaseHelper.java:22)
                                                                      at com.example.hp.teachme.Home.onCreate(Home.java:18)
                                                                      at android.app.Activity.performCreate(Activity.java:6285)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524) 
                                                                      at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:234) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:5526) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:1)

我通过简单地修改“DatabaseHelper”类中的这一行解决了这个问题:

public static final String TABLE_NAME = "prof_table.db";

并改为:

public static final String TABLE_NAME = "prof_table";

愚蠢的错误真的好笑