我想创建一个数据库,运行LogCat后我得到以下错误:
我的代码如下:
Databasehandler.java
package com.androidhive.androidsqlite;
public class DatabaseHandler extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "PositionManager";
// Contacts table name
private static final String TABLE_POSITIONS = "positions";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "Name";
private static final String KEY_KATEGORIE = "kategorie";
private static final String KEY_LAENGE = "Laenge";
private static final String KEY_BREITE = "Breite";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_POSITIONS + "("+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_KATEGORIE + " TEXT," + KEY_LAENGE + " TEXT,"+ KEY_BREITE + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_POSITIONS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addPosition(Position position) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, position.getName()); // Name
values.put(KEY_KATEGORIE, position.getkategorie()); // Kategorie
values.put(KEY_BREITE, position.getBREITE()); // Breite
values.put(KEY_BREITE, position.getLAENGE()); // Länge
// Inserting Row
db.insert(TABLE_POSITIONS, null, values);
db.close(); // Closing database connection
}
// Getting single position
Position getPosition(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_POSITIONS, new String[] { KEY_ID, KEY_NAME, KEY_KATEGORIE, KEY_LAENGE, KEY_BREITE }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Position position = new Position(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4));
// return position
return position;
}
// Getting All Positions
public List<Position> getAllPositions() {
List<Position> positionList = new ArrayList<Position>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_POSITIONS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Position position = new Position();
position.setID(Integer.parseInt(cursor.getString(0)));
position.setName(cursor.getString(1));
position.setkategorie(cursor.getString(2));
position.setLAENGE(cursor.getString(3));
position.setBREITE(cursor.getString(4));
// Adding position to list
positionList.add(position);
} while (cursor.moveToNext());
}
// return position list
return positionList;
}
// Updating single contact
public int updatePosition(Position position) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, position.getName());
values.put(KEY_KATEGORIE, position.getkategorie());
values.put(KEY_LAENGE, position.getLAENGE());
values.put(KEY_BREITE, position.getBREITE());
// updating row
return db.update(TABLE_POSITIONS, values, KEY_ID + " = ?", new String[] { String.valueOf(position.getID()) });
}
// Deleting single position
public void deletePosition(Position position) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_POSITIONS, KEY_ID + " = ?", new String[] { String.valueOf(position.getID()) });
db.close();
}
// Getting positions Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_POSITIONS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
AndroidSQLiteTutorialActivity.java
package com.androidhive.androidsqlite;
public class AndroidSQLiteTutorialActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Positions
Log.d("Insert: ", "Inserting ..");
db.addPosition(new Position("Hobbersdorfer Felder", "Landschaft" , "53.94966", "10.70343"));
db.addPosition(new Position("Felder bei Pelzerhaken", "Landschaft", "54.10039", "10.83878"));
db.addPosition(new Position("Felder bei Neustadt", "Landschaft" , "54.10713", "10.84201"));
db.addPosition(new Position("Felder bei Gronenberg", "Landschaft" , "54.04454", "10.69646"));
// Reading all positions
Log.d("Reading: ", "Reading all positions..");
List<Position> positions = db.getAllPositions();
for (Position cn : positions) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() +" ,Kategorie: " + cn.getkategorie() + " ,Länge: " + cn.getLAENGE()+" ,Breite: " + cn.getBREITE();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
}
Position.java
package com.androidhive.androidsqlite;
public class Position {
//private variables
int _id;
String kategorie;
String name;
String breite;
String laenge;
// Empty constructor
public Position(){
}
// constructor
public Position(int id, String kategorie, String name, String breite, String laenge){
this._id = id;
this.kategorie = kategorie;
this.name = name;
this.breite = breite;
this.laenge = laenge;
}
// constructor
public Position(String kategorie, String name, String breite, String laenge){
this.kategorie = kategorie;
this.name = name;
this.breite = breite;
this.laenge = laenge;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting category
public String getkategorie(){
return this.name;
}
// setting category
public void setkategorie(String kategorie){
this.kategorie = kategorie;
}
// getting name
public String getName(){
return this.name;
}
// setting name
public void setName(String name){
this.name = name;
}
// getting BREITE
public String getBREITE(){
return this.breite;
}
// setting BREITE
public void setBREITE(String breite){
this.breite = breite;
}
// getting LAENGE
public String getLAENGE(){
return this.laenge;
}
// setting LAENGE
public void setLAENGE(String laenge){
this.laenge = laenge;
}
}
答案 0 :(得分:0)
在研究你的代码后,我发现了下一个错误。 我不知道它会在你的问题中帮助你,但你应该在接下来纠正:
void addPosition(Position position) {
SQLiteDatabase db = this.getWritableDatabase();
// code
values.put(KEY_BREITE, position.getBREITE()); // Breite
values.put(KEY_BREITE, position.getLAENGE()); // Länge
// code
db.close(); // Closing database connection
}
这里有复制粘贴错误。将KEY_BREITE
替换为KEY_LAENGE