📄 dbhelper.java
字号:
package lordhong.apps;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DBHelper {
static String LOGGER = "mini.gmap.DBHelper";
class AddressRow extends Object {
public long rowId;
public String address;
public long latitude;
public long longitude;
}
private static final String DATABASE_CREATE =
"create table addresses (rowid integer primary key autoincrement, "
+ "address text not null, latitude integer, longitude integer);";
private static final String DATABASE_NAME = "minigmap091";
private static final String DATABASE_TABLE = "addresses";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase db;
public DBHelper(Context ctx) {
try {
db = ctx.openDatabase(DATABASE_NAME, null);
} catch (FileNotFoundException e) {
try {
db =
ctx.createDatabase(DATABASE_NAME, DATABASE_VERSION, 0,
null);
db.execSQL(DATABASE_CREATE);
// empire state building
createRow("350 5th Ave, New York, NY 10118", 40748356, -73984621);
// grand central station
createRow("15 Vanderbilt Ave, New York, NY 10017", 40752831, -73977864);
// the white house
createRow("1600 Pennsylvania Avenue NW, Washington, DC 20500", 38898590, -77036473);
} catch (FileNotFoundException e1) {
Log.e(LOGGER, e1.toString());
db = null;
}
}
}
public void close() {
db.close();
}
public void createRow(String address, long latitude, long longitude) {
ContentValues initialValues = new ContentValues();
initialValues.put("address", address);
initialValues.put("latitude", latitude);
initialValues.put("longitude", longitude);
db.insert(DATABASE_TABLE, null, initialValues);
}
public void deleteRow(long rowId) {
db.delete(DATABASE_TABLE, "rowid=" + rowId, null);
}
public List<AddressRow> fetchAllRows() {
ArrayList<AddressRow> ret = new ArrayList<AddressRow>();
try {
Cursor c =
db.query(DATABASE_TABLE, new String[] {
"rowid", "address", "latitude", "longitude"}, null, null, null, null, null);
int numRows = c.count();
c.first();
for (int i = 0; i < numRows; ++i) {
AddressRow row = new AddressRow();
row.rowId = c.getLong(0);
row.address = c.getString(1);
row.latitude = c.getLong(2);
row.longitude = c.getLong(3);
ret.add(row);
c.next();
}
} catch (SQLException e) {
Log.e(LOGGER, e.toString());
}
return ret;
}
public AddressRow fetchRow(long rowId) {
AddressRow row = new AddressRow();
Cursor c =
db.query(true, DATABASE_TABLE, new String[] {
"rowid", "address", "latitude", "longitude"}, "rowid=" + rowId, null, null,
null, null);
if (c.count() > 0) {
c.first();
row.rowId = c.getLong(0);
row.address = c.getString(1);
row.latitude = c.getLong(2);
row.longitude = c.getLong(3);
return row;
} else {
row.rowId = -1;
row.address = null;
row.latitude = 0;
row.longitude = 0;
}
return row;
}
public AddressRow fetchRow(String address) {
Log.e(LOGGER, "fetch row: " + address);
AddressRow row = new AddressRow();
Cursor c =
db.query(false, DATABASE_TABLE, new String[] {
"rowid", "address", "latitude", "longitude"}, "address='" + address+"'", null, null,
null, null);
if (c.count() > 0) {
Log.e(LOGGER, "count > 0 ");
c.first();
row.rowId = c.getLong(0);
row.address = c.getString(1);
row.latitude = c.getLong(2);
row.longitude = c.getLong(3);
return row;
} else {
Log.e(LOGGER, "count == 0 ");
row.rowId = -1;
row.address = null;
row.latitude = 0;
row.longitude = 0;
}
return row;
}
public void updateRow(long rowId, String address, long latitude, long longitude) {
ContentValues args = new ContentValues();
args.put("address", address);
args.put("latitude", latitude);
args.put("longitude", longitude);
db.update(DATABASE_TABLE, args, "rowid=" + rowId, null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -