我想创建一个方法,在我的数据库中搜索一个类别,例如可以是横向,教堂或其他东西。
我已经发现,我将需要SQL查询。但是如何在我的数据库中使用它?
这是正确的代码吗?如果有人能尽快帮助我,我会很高兴:)
SELECT * FROM [位置] 在哪里Kategorie ='Landschaft' - > category = landscape(英文)
以下是我的数据库的代码:
package com.example.fotooh2;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 4;
// Database Name
private static final String DATABASE_NAME = "PositionManager";
// Positions table name
private static final String TABLE_POSITIONS = "positions";
// Positions 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_LAENGE, position.getLaenge()); // Länge
values.put(KEY_BREITE, position.getBreite()); // Breite
// 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 position
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 getPositionsCount() {
String countQuery = "SELECT * FROM " + TABLE_POSITIONS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}