package com.owen.quartergames.dao;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import com.owen.quartergames.R;
import com.owen.quartergames.domain.LogEntry;
public class SqlLiteFishLoggerDao extends SQLiteOpenHelper implements
FishLoggerDao {
private static final String DB_NAME = "fishingLog";
private static final String TABLE_NAME = "LogEntries";
private static final String DELETE_LOG_ENTRY_SQL = "DELETE FROM LogEntries WHERE _id = ?;";
private static final String FIND_LOG_ENTRY_SQL = "SELECT _id, Longitude, Latitude FROM LogEntries WHERE _id = ?";
private static final String FIND_ALL_ENTRIES_SQL = "SELECT * FROM LogEntries";
private static final String[] NO_ARGS = {};
private Context context;
private final SQLiteDatabase db = getWritableDatabase();
public SqlLiteFishLoggerDao(Context context) {
super(context, DB_NAME, null, 1);
this.context = context;
}
@Override
public void deleteLogEntry(String id) {
id = "0";
db.execSQL(DELETE_LOG_ENTRY_SQL, new Object[] { id });
// int deleted = db.delete(TABLE_NAME, "_id = ?",
// new String[] { id.trim() });
// Log.i("fishlogger", String.format("Delete %d rows", deleted));
db.close();
}
@Override
public LogEntry findEntry(String id) {
Cursor cursor = db.rawQuery(FIND_LOG_ENTRY_SQL, new String[] { id });
if (!cursor.moveToFirst()) {
return null;
}
LogEntry entry = new LogEntry();
entry.setId(id);
entry.setLatitude(cursor.getDouble(cursor.getColumnIndex("Latitude")));
entry
.setLongitude(cursor.getDouble(cursor
.getColumnIndex("Longitude")));
cursor.close();
db.close();
return entry;
}
@Override
public void insertLogEntry(LogEntry entry) {
ContentValues values = new ContentValues();
values.put("Latitude", entry.getLatitude());
values.put("Longitude", entry.getLongitude());
values.put("PictureURL", entry.getPictureUrl());
values.put("SizeOrWeight", entry.getSizeOrWeight());
values.put("CreateDate", entry.getEntryDate());
values.put("Species", entry.getSpecies());
db.insertOrThrow("LogEntries", null, values);
db.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
String s;
try {
Toast.makeText(context, "1", 2000).show();
InputStream in = context.getResources().openRawResource(R.raw.sql);
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList statements = doc.getElementsByTagName("statement");
for (int i = 0; i < statements.getLength(); i++) {
s = statements.item(i).getChildNodes().item(0).getNodeValue();
db.execSQL(s);
}
} catch (Throwable t) {
Toast.makeText(context, t.toString(), 50000).show();
}
Log.e("DB", "DB Created");
}
@Override
public List<LogEntry> findAllEntries() {
List<LogEntry> entries = new ArrayList<LogEntry>();
Cursor cursor = db.rawQuery(FIND_ALL_ENTRIES_SQL, NO_ARGS);
int entryDateCol = cursor.getColumnIndex("CreateDate");
int speciesCol = cursor.getColumnIndex("Species");
int sizeCol = cursor.getColumnIndex("SizeOrWeight");
int latCol = cursor.getColumnIndex("Latitude");
if (cursor.moveToFirst()) {
do {
LogEntry entry = new LogEntry();
entry.setEntryDate(cursor.getString(entryDateCol));
entry.setSpecies(cursor.getString(speciesCol));
entry.setSizeOrWeight(cursor.getString(sizeCol));
entry.setLatitude(cursor.getDouble(latCol));
if (entry.getSpecies() == null) {
entry.setSpecies("Not Entered");
}
if (entry.getSizeOrWeight() == null) {
entry.setSizeOrWeight("Not entered");
}
entries.add(entry);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return entries;
}
@Override
public void onUpgrade(SQLiteDatabase DB, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(getWritableDatabase());
}
}
基本上是删除不工作但我认为,问题的findAllEntries()方法的茎。我说的原因是,我得到漏找到日志猫的错误说法在创建数据库,但永远不会关闭。我已经透了在网上搜索,并要求朋友谁code,但在解决问题,没有运气。我所有的游标被关闭,从我可以告诉这是怎样一个sqlliteopenhelper一般应该是什么样子。
basically the delete doesnt work however i think the problems stems from the findAllEntries() method. the reason i say that is that i get "leak found" errors in log cat saying the db was created but never closed. i have throughly searched the internet and have asked friends who code but no luck in solving the problem. all my cursors are closed and from what i can tell this is how a sqlliteopenhelper should generally look like.
感谢您的回复
只是想随动。我感谢大家的帮助。 在最后,似乎有几个问题。即使世界很少有机会能跟大家可能已经想通了乌尔自己什么,我给ü我花了永远的数字出来。 反正继承人我做错了什么,加上更新code:
just wanted to followup. i thank you all for your help. in the end it seems there were a few issues. Theres very little chance u guys could have figured it out on ur own with what i gave u it took me forever to figure it out. anyways heres what i did wrong plus the updated code:
我不是打开和关闭所述DB中的每 方法(泄漏发现错误)
I wasnt opening and closing the DB for every method (leaks found errors)
findallentries不喂 _id字段插入对象。所以当我的列表视图被填充和u 说删除,它将使用 迭代id作为相对于真实 条目的数据库_id。
findallentries was not feeding the _id field into the object. and so when my listview was populated and u said delete, it would use the iterator id as opposed to the real database _id of the entry.
我需要调用.trim本人身份证 现场
i needed to call .trim on my id field
(包括2和3但是生产logcat中没有任何错误。它只是永远不会删除。它插入精细,选择*好的,但什么也没发生在删除)
(both 2 and 3 however produced no errors in logcat. it would just never delete. it inserted fine, select * fine, but nothing happened on delete)
package com.owen.quartergames.dao;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.owen.quartergames.R;
import com.owen.quartergames.domain.LogEntry;
public class SqlLiteFishLoggerDao extends SQLiteOpenHelper implements
FishLoggerDao {
private static final String DB_NAME = "fishingLog";
private static final String TABLE_NAME = "LogEntries";
private static final String DELETE_LOG_ENTRY_SQL = "DELETE FROM LogEntries WHERE _id = ?;";
private static final String FIND_LOG_ENTRY_SQL = "SELECT _id, Longitude, Latitude FROM LogEntries WHERE _id = ?";
private static final String FIND_ALL_ENTRIES_SQL = "SELECT * FROM LogEntries";
private static final String[] NO_ARGS = {};
private Context context;
private SQLiteDatabase DB;
public SqlLiteFishLoggerDao(Context context) {
super(context, DB_NAME, null, 1);
this.context = context;
}
@Override
public void deleteLogEntry(String id) {
DB = getWritableDatabase();
DB.execSQL(DELETE_LOG_ENTRY_SQL, new Object[] { id });
DB.close();
}
@Override
public LogEntry findEntry(String id) {
DB = getReadableDatabase();
Cursor cursor = DB.rawQuery(FIND_LOG_ENTRY_SQL,
new String[] { id });
if (!cursor.moveToFirst()) {
return null;
}
LogEntry entry = new LogEntry();
entry.setId(id);
entry.setLatitude(cursor.getDouble(cursor.getColumnIndex("Latitude")));
entry
.setLongitude(cursor.getDouble(cursor
.getColumnIndex("Longitude")));
cursor.close();
DB.close();
return entry;
}
@Override
public void insertLogEntry(LogEntry entry) {
DB = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Latitude", entry.getLatitude());
values.put("Longitude", entry.getLongitude());
values.put("PictureURL", entry.getPictureUrl());
values.put("SizeOrWeight", entry.getSizeOrWeight());
values.put("CreateDate", entry.getEntryDate());
values.put("Species", entry.getSpecies());
DB.insertOrThrow("LogEntries", null, values);
DB.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
String s;
try {
InputStream in = context.getResources().openRawResource(R.raw.sql);
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList statements = doc.getElementsByTagName("statement");
for (int i = 0; i < statements.getLength(); i++) {
s = statements.item(i).getChildNodes().item(0).getNodeValue();
db.execSQL(s);
}
Log.e("DB", "DB Created Successfully");
} catch (Throwable t) {
Log.e("DB error: ",t.toString());
}
}
@Override
public List<LogEntry> findAllEntries() {
DB = getReadableDatabase();
List<LogEntry> entries = new ArrayList<LogEntry>();
Cursor cursor = DB.rawQuery(FIND_ALL_ENTRIES_SQL,
NO_ARGS);
int entryID = cursor.getColumnIndex("_id");
int entryDateCol = cursor.getColumnIndex("CreateDate");
int speciesCol = cursor.getColumnIndex("Species");
int sizeCol = cursor.getColumnIndex("SizeOrWeight");
int latCol = cursor.getColumnIndex("Latitude");
if (cursor.moveToFirst()) {
do {
LogEntry entry = new LogEntry();
entry.setId(cursor.getString(entryID));
entry.setEntryDate(cursor.getString(entryDateCol));
entry.setSpecies(cursor.getString(speciesCol));
entry.setSizeOrWeight(cursor.getString(sizeCol));
entry.setLatitude(cursor.getDouble(latCol));
if (entry.getSpecies() == null) {
entry.setSpecies("Not Entered");
}
if (entry.getSizeOrWeight() == null) {
entry.setSizeOrWeight("Not entered");
}
entries.add(entry);
} while (cursor.moveToNext());
}
cursor.close();
DB.close();
return entries;
}
@Override
public void onUpgrade(SQLiteDatabase DB, int oldVersion, int newVersion) {
DB = getWritableDatabase();
DB.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(getWritableDatabase());
DB.close();
}
}