Android SQLite ....没有这样的表:场地(代码1):,同时编译

时间:2014-07-22 14:23:55

标签: android database sqlite

在这里,我正在尝试从预先构建的SQLite DB中获取数据..... 我正在从这个网站上获取代码....    http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

现在的问题是我最终没有这样的表格问题......我已经在资源文件夹中添加了我的表...!

Screenshot of my db

这是DBHelper类

 public class DataBaseHelper extends SQLiteOpenHelper{

//The Android's default system path of your application database.

private static String DB_PATH = "/data/data/com.prokode.cwg2014/assets/";

private static String DB_NAME = "glasgow.db";

private static final String TABLE_VENUE = "venue";

public static final int DATABASE_VERSION = 1;


private static final String KEY_ID = "_id";
private static final String KEY_NAME = "name";
private static final String KEY_IMG = "img";
private static final String KEY_CAPACITY = "capacity";
private static final String KEY_GAMES = "games";
private static final String KEY_LOCATION = "location";
private static final String KEY_LINK = "link";
private static final String KEY_GEO = "geo";

private SQLiteDatabase myDataBase;

private final Context myContext;



public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, DATABASE_VERSION);

    this.myContext = context;

}
    @Override

public void onCreate(SQLiteDatabase db) {

}

@Override

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

}


//Getting single venue
public VenueInfoDS getVenueInfo(String id) {

SQLiteDatabase db = this.getReadableDatabase();

Cursor cursor = db.query(TABLE_VENUE, new String[] { KEY_ID,KEY_NAME, KEY_IMG,
        KEY_CAPACITY,KEY_GAMES,KEY_LOCATION, 
        KEY_LINK, KEY_GEO }, KEY_ID + "=?",new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
    cursor.moveToFirst();

VenueInfoDS venueInfo = new VenueInfoDS(cursor.getString(0),
        cursor.getString(1), cursor.getString(2),cursor.getString(3),cursor.getString(4),
        cursor.getString(5),cursor.getString(6),cursor.getString(7));
// return venueInfo
return venueInfo;
}

//Getting All venues
  public List<VenueInfoDS> getAllVenueInfo() {

List<VenueInfoDS> venueInfoList = new ArrayList<VenueInfoDS>();
 // Select All Query
String selectQuery = "SELECT  * FROM " + TABLE_VENUE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
 // looping through all rows and adding to list
if (cursor.moveToFirst()) {
    do {
        VenueInfoDS venue = new VenueInfoDS();
        venue.set_id(cursor.getString(0));
        venue.set_name(cursor.getString(1));
        venue.set_img(cursor.getString(2));
        venue.set_capacity(cursor.getString(3));
        venue.set_games(cursor.getString(4));
        venue.set_location(cursor.getString(5));
        venue.set_link(cursor.getString(6));
        venue.set_geo(cursor.getString(7));
        // Adding contact to list
        venueInfoList.add(venue);
    } while (cursor.moveToNext());
}
//return venueInfo list
return venueInfoList;
 }

//Getting venues Count
public int getVenueInfoDSCount() {
 String countQuery = "SELECT  * FROM " + TABLE_VENUE;
 SQLiteDatabase db = this.getReadableDatabase();
 Cursor cursor = db.rawQuery(countQuery, null);
 cursor.close();

 // return count
 return cursor.getCount();
}

} 

这是我的片段类......

 public class VenueInfoFragment extends Fragment {
String title, capacity, games, location, image;
TextView venueTitle, capacityValue, gamesValue, locationValue;
ImageView venueImage;
DataBaseHelper dbHelper;
VenueInfoDS venueInfo;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    LinearLayout fragmentLayout = (LinearLayout) inflater.inflate(R.layout.venue_info_fragment, container, false);

    return fragmentLayout;
}


public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    venueTitle = (TextView) getActivity().findViewById(R.id.textView_venueTitle);
    capacityValue = (TextView) getActivity().findViewById(R.id.textView_capacityValue);
    gamesValue = (TextView) getActivity().findViewById(R.id.textView_gamesValue);
    locationValue = (TextView) getActivity().findViewById(R.id.textView_locationValue);
    venueImage = (ImageView) getActivity().findViewById(R.id.imageView_venue);
    dbHelper = new DataBaseHelper(getActivity());

    venueInfo = dbHelper.getVenueInfo("even1");

    title = venueInfo.get_name();

    venueTitle.setText(title);



}

}

LOG CAT ----

 07-22 19:22:20.052: E/AndroidRuntime(1001): FATAL EXCEPTION: main
 07-22 19:22:20.052: E/AndroidRuntime(1001): android.database.sqlite.SQLiteException:      no such table: venue (code 1): , while compiling: SELECT _id, name, img, capacity, games,  location, link, geo FROM venue WHERE _id=?
 07-22 19:22:20.052: E/AndroidRuntime(1001):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
 07-22 19:22:20.052: E/AndroidRuntime(1001):    at    android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)

1 个答案:

答案 0 :(得分:0)

DB_PATH变量未在任何地方使用。 Android不知道您的资源文件夹中是否存在数据库。相反,它会在/data/data/<your.package.name>/databases/内创建一个新的,并期望您创建架构并使用数据填充它。这就是SQLiteOpenHelper的工作方式。

要使用现有数据库,您需要在尝试使用数据库之前将数据库从assets文件夹复制到设备上的应用程序内部数据目录。您链接到的教程未显示如何执行此操作。像这个库这样的东西可能更合适:

https://github.com/jgilfelt/android-sqlite-asset-helper