我已经完成了使用SQLite,但我无法存储值。我通过ListView
检索值来检查这一点。有人可以帮忙吗?我很震惊这个项目。
package com.mahesh.map2;
public class history extends ListActivity {
//private static final String DATABASE_TABLE = null;
private ArrayList<String> results = new ArrayList<String>();
// private static final String DATABASE_TABLE = "locations";
private static String DBNAME = "locationmarkersqlite";
/** Field 1 of the table locations, which is the primary key */
public static final String FIELD_ROW_ID = "_id";
/** Field 2 of the table locations, stores the latitude */
public static final String FIELD_LAT = "lat";
/** Field 3 of the table locations, stores the longitude*/
public static final String FIELD_LNG = "lng";
/** Field 4 of the table locations, stores the zoom level of map*/
public static final String FIELD_ZOOM = "zom";
/** A constant, stores the the table name */
private static final String DATABASE_TABLE = "locations";
/** An instance variable for SQLiteDatabase */
private SQLiteDatabase mDB;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
TextView tView = new TextView(this);
tView.setText("This data is retrieved from the database and only 4 " +
"of the results are displayed");
getListView().addHeaderView(tView);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase() {
try {
LocationsDB dbHelper = new LocationsDB(this.getApplicationContext());
mDB = dbHelper.getWritableDatabase();
//double latitude;
//double longitude;
Cursor c = mDB.query("locations", new String[]{
"latitude", "longitude"}, null, null,
null, null, null);
if (c != null ) {
if (c.moveToFirst()) {
do {
// String title = c.String(c
// .getColumnIndex("title"));
// String description = c.String(c
// .getColumnIndex("description"));
int latitude = (int) (c.getDouble(c
.getColumnIndex("latitude")) * 1E6);
int longitude = (int) (c.getDouble(c
.getColumnIndex("longitude")) * 1E6);
results.add("FIELD_LNG" + latitude + ",FIELD_LAT " + longitude);
}while (c.moveToNext());
}
}
} catch (SQLiteException e ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (mDB != null)
mDB.execSQL("DELETE FROM " + DATABASE_TABLE);
mDB.close();
}
}
}
答案 0 :(得分:0)
我正在使用ORMLite库来管理Db的东西。它很容易实现,不应超过4小时,但将来会非常有价值。首先,您必须创建一些要存储在db中的对象并添加注释,以便ORMLite知道存储的内容和方式。
public class MyTableRow {
@DatabaseField
long longitude;
@DatabaseField
long latitude;
@DatabaseField(id=true)
int id;
public MyTableRow(long lon,long lat){..};
}
之后,创建连接源:
MySqliteOpenHelper dbHelper = null;
AndroidConnectionSource connectionSource = null;
if (dbHelper == null || connectionSource == null) {
dbHelper = new MySqliteOpenHelper(context, null, null,
0);
connectionSource = new AndroidConnectionSource(dbHelper);
}
MySqliteOpenHelper可以是一个简单的配置类,具有数据库名称和版本:
public class MySqliteOpenHelper extends OrmLiteSqliteOpenHelper {
private static final String DATABASE_NAME = "test";
private static final int DATABASE_VERSION = 2;
public MySqliteOpenHelper(Context context, String databaseName,
CursorFactory factory, int databaseVersion) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
}
@Override
public void onUpgrade(SQLiteDatabase database,
ConnectionSource connectionSource, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
设置好后,您可以开始创建表格并插入和检索数据:
public static void createTables(ConnectionSource connectionSource,
Class<?>... classes) {
for (Class<?> c : classes) {
try {
TableUtils.createTableIfNotExists(connectionSource, c);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static <T> void doInserts(ConnectionSource connectionSource,
Class<T> object, List<T> objects)
throws SQLException {
final Dao<T, String> dao = DaoManager.createDao(connectionSource,
object);
for (T data : objects) {
dao.createOrUpdate(data);
}
}
public static <T> List<T> getTable(Class<T> objectClass,
Context context) {
MySqliteOpenHelper sqliteDbHelper = new MySqliteOpenHelper(context,
null, null, 0);
ConnectionSource connectionSource = new AndroidConnectionSource(
sqliteDbHelper);
List<T> mList;
try {
Dao<T, String> mDao = DaoManager.createDao(connectionSource,
objectClass);
mList = mDao.queryForAll();
return mList;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
return null;
}