找不到sqlite数据库错误表

时间:2014-04-15 05:33:09

标签: android database sqlite

我尝试使用适配器在listview中显示我的sqlitedatabase中的数据但是找不到错误表和nullpointerexception,请快速回复..谢谢

`package utils;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;



import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;


public class DBhelper extends SQLiteOpenHelper {



private final static String DATABASE_NAME = "dataofg.sqlite";
private String DATABASE_PATH = "data/data/com.example.gindicator/databases/";

private final static int DATABASE_VERSION = 4;

private Context mContext;
private SQLiteDatabase sqliteDatabase = null;

public DBhelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

    mContext = context;

    // TODO Auto-generated constructor stub
}

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase() {

    try {
        String myPath = DATABASE_PATH + DATABASE_NAME;
        sqliteDatabase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
    }
    if (sqliteDatabase != null) {
        sqliteDatabase.close();
    }
    return sqliteDatabase != null ? true : false;
}

private void copyDataBase() throws IOException {

    InputStream myInput = mContext.getAssets().open(DATABASE_NAME);
    String outFileName = DATABASE_PATH + DATABASE_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    byte[] buffer = new byte[1024];
    int length;

    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException {
    String myPath = DATABASE_PATH + DATABASE_NAME;
    sqliteDatabase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
}

public DBhelper open() throws SQLException {
    sqliteDatabase = getWritableDatabase();
    return this;
}

@Override
public synchronized void close() {

    if (sqliteDatabase != null)
        sqliteDatabase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

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

}



public void GetStationData()  {
    String StrQuery = "Select * from tblStations";

    Constant.ArrStationName.clear();
    open();
    Cursor cursor = sqliteDatabase.rawQuery(StrQuery, null);
    //Log.e("cursor", "" + cursor.getCount());
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        do {

    // Log.i("City","" +               
   cursor.getString(cursor.getColumnIndex("strStationName")));
            Constant.ArrStationName.add(cursor.getString(cursor
                    .getColumnIndex("strStationName")));
        } while (cursor.moveToNext());

    }
    cursor.close();
close();

}`

这是DBhelper calss,下面是我定义的数组

`package utils;

import java.util.ArrayList;

public class Constant {


public static ArrayList<String> ArrStationName = new ArrayList<String>();
}

` 适配器类

`package com.gindicator.adapter;



import com.example.gindicator.R;

import utils.Constant;
import android.content.Context;
import android.view.LayoutInflater;    
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CitybusAdapter extends BaseAdapter {
Context context;
LayoutInflater LI;

public CitybusAdapter(Context contextpara) {
    context = contextpara;
    LI = LayoutInflater.from(contextpara);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return Constant.ArrStationName.size();
}

@Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return Constant.ArrStationName.size();
}

@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    if (convertView == null) {
        convertView = LI.inflate(R.layout.city_raw, null);
        TextView txtCityRaw = (TextView) convertView
                .findViewById(R.id.txtCityRaw);
        txtCityRaw.setText(Constant.ArrStationName.get(position));

    }
    return convertView;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

}`

主要类,我在数据库中显示数据

`package com.example.gindicator;

import java.io.IOException;

import com.gindicator.adapter.CitybusAdapter;



import utils.Constant;
import utils.DBhelper;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;


public class Citybusbtn1 extends Activity{
public static DBhelper myDbHelper;
ListView lst ;
CitybusAdapter cityobject;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.citybusbtn1);


        cityobject = new CitybusAdapter(Citybusbtn1.this);
        DBhelper myDbHelper = new DBhelper(this);

        try {
            myDbHelper.createDataBase();

            // myDbHelper.openDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            myDbHelper.GetStationData();


        Initialization();


    }



    private void Initialization() {
    // TODO Auto-generated method stub

        if (Constant.ArrStationName.size() > 0) {
            lst=(ListView)findViewById(R.id.hotellist);
            lst.setAdapter(cityobject);
        }
}




    @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;
    }

}
`

当我第一次运行程序时,它会在第1页中运行时在日志和停止程序中显示错误

    04-16 11:27:45.702: D/dalvikvm(14822): GC_CONCURRENT freed 0K, 7% free    
     16033K/17223K,      
  paused 1ms+1ms, total 34ms
   04-16 11:27:45.702: D/dalvikvm(14822): WAIT_FOR_CONCURRENT_GC blocked 15ms
   04-16 11:27:45.706: E/SQLiteLog(14822): (14) cannot open file at line 30174 of        
  [00bb9c9ce4]
  04-16 11:27:45.706: E/SQLiteLog(14822): (14) os_unix.c:30174: (2) 
  open(//data/data/com.example.gindicator/databases/dataofg.sqlite) - 
 04-16 11:27:45.709: E/SQLiteDatabase(14822): Failed to open database 
  'data/data/com.example.gindicator/databases/dataofg.sqlite'.
 04-16 11:27:45.709: E/SQLiteDatabase(14822):   
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could 
not    
open database

之后显示错误就像这个logcat whwn我点击按钮显示数据列表视图

04-16 11:15:02.925: V/Provider/Setting(14024): from settings cache , name =                

 sound_effects_enabled value = 0
04-16 11:15:02.986: D/AbsListView(14024): checkAbsListViewlLogProperty get invalid     

command
 04-16 11:15:03.002: E/SQLiteLog(14024): (1) no such table: tblStations
  04-16 11:15:03.002: D/AndroidRuntime(14024): Shutting down VM
  04-16 11:15:03.003: W/dalvikvm(14024): threadid=1: thread exiting with uncaught             
  exception (group=0x40f61908)
  04-16 11:15:03.011: E/AndroidRuntime(14024): FATAL EXCEPTION: main  
  04-16 11:15:03.011: E/AndroidRuntime(14024): java.lang.RuntimeException: Unable to    
   start activity 
   ComponentInfo{com.example.gindicator/com.example.gindicator.Citybusbtn1}: 

     android.database.sqlite.SQLiteException: no such table: tblStations (code 1): , 

      while compiling: Select * from tblStations

      04-16 11:15:03.011: E/AndroidRuntime(14024):  at 

      android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2189)


        04-16 11:15:03.011: E/AndroidRuntime(14024):    at 

          android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2216)


          04-16 11:15:03.011: E/AndroidRuntime(14024):  at 

         android.app.ActivityThread.access$600(ActivityThread.java:149)
 04-16 11:15:03.011: E/AndroidRuntime(14024):   at         
  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at 
 android.os.Handler.dispatchMessage(Handler.java:99)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at 
  android.os.Looper.loop(Looper.java:153)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at         
  android.app.ActivityThread.main(ActivityThread.java:5000)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at 
 java.lang.reflect.Method.invokeNative(Native Method)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at
   java.lang.reflect.Method.invoke(Method.java:511)

   04-16 11:15:03.011: E/AndroidRuntime(14024):     at 
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at    
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)

   04-16 11:15:03.011: E/AndroidRuntime(14024):     at 
  dalvik.system.NativeStart.main(Native Method)
   04-16 11:15:03.011: E/AndroidRuntime(14024): Caused by:       
  android.database.sqlite.SQLiteException: no such table: tblStations (code 1): , while          
   compiling: Select * from tblStations
   04-16 11:15:03.011: E/AndroidRuntime(14024):     at 

   android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
     04-16 11:15:03.011: E/AndroidRuntime(14024):   at 

    android.database.sqlite.SQLiteConnection.acquirePreparedStatement
  (SQLiteConnection.java:882  
      )
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at        
   android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
    04-16 11:15:03.011: E/AndroidRuntime(14024):    at 
     android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    04-16 11:15:03.011: E/AndroidRuntime(14024):    at      
   android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    04-16 11:15:03.011: E/AndroidRuntime(14024):    at 
     android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
     04-16 11:15:03.011: E/AndroidRuntime(14024):   at 
     android.database.sqlite.SQLiteDirectCursorDriver.query
  (SQLiteDirectCursorDriver.java:44)
   04-16 11:15:03.011: E/AndroidRuntime(14024):     at       
   android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
    04-16 11:15:03.011: E/AndroidRuntime(14024):    at   
      android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)

    04-16 11:15:03.011: E/AndroidRuntime(14024):    at 
    utils.DBhelper.GetStationData(DBhelper.java:123)
   04-16 11:15:03.011: E/AndroidRuntime(14024):     at    
   com.example.gindicator.Citybusbtn1.onCreate(Citybusbtn1.java:40)

   04-16 11:15:03.011: E/AndroidRuntime(14024):     at 
  android.app.Activity.performCreate(Activity.java:5020)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at 
  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  04-16 11:15:03.011: E/AndroidRuntime(14024):  at   
  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
 04-16 11:15:03.011: E/AndroidRuntime(14024):   ... 11 more
 04-16 11:15:04.696: I/Process(14024): Sending signal. PID: 14024 SIG: 9

谢谢你的朋友

1 个答案:

答案 0 :(得分:0)

尝试从其他表中获取数据。如果它正常工作删除表并重新创建。如果没有,请检查数据库的安全性和Android项目的身份验证。