对于我的实习,我正在开发一个Android应用程序,并试图让一个本地数据库运行以设置离线referal,以及每次用户返回某个活动时阻止新的BasicAuth请求和所需的数据库拉取。为了达到这个目的,我正在尝试建立一个SQLite数据库,但事情并没有真正解决。
我目前正在使用this教程来尝试设置数据库。对于我的应用程序,我专门编辑了MySQLiteHelper和TicketsDataSource类。
MySQLiteHelper:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_TICKETS = "tickets";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TICKET_ID = "ticketId";
public static final String COLUMN_TICKET_TITLE = "ticketTitle";
public static final String COLUMN_PROJECT_NAME = "projectName";
public static final String COLUMN_CLIENT_NAME = "clientName";
public static final String COLUMN_TICKET_DESCRIPTION = "ticketDescription";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
// Database creation SQL statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_TICKETS
+ "("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_TICKET_ID + " text not null, "
+ COLUMN_TICKET_TITLE+ " text not null, "
+ COLUMN_PROJECT_NAME+ " text not null, "
+ COLUMN_CLIENT_NAME+ " text not null, "
+ COLUMN_TICKET_DESCRIPTION+ " text not null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TICKETS);
onCreate(db);
}
}
TicketsDataSource:
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;
public class TicketsDataSource {
// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_TICKET_ID, MySQLiteHelper.COLUMN_TICKET_TITLE, MySQLiteHelper.COLUMN_PROJECT_NAME,
MySQLiteHelper.COLUMN_CLIENT_NAME, MySQLiteHelper.COLUMN_TICKET_DESCRIPTION};
public TicketsDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Ticket createTicket(String ticket) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_TICKET_DESCRIPTION, ticket);
long insertId = database.insert(MySQLiteHelper.TABLE_TICKETS, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_TICKETS,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Ticket newTicket = cursorToTicket(cursor);
cursor.close();
return newTicket;
}
public void deleteTicket(Ticket ticket) {
long id = ticket.getId();
System.out.println("Ticket deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_TICKETS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}
public List<Ticket> getAllTickets() {
List<Ticket> tickets = new ArrayList<Ticket>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_TICKETS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Ticket ticket = cursorToTicket(cursor);
tickets.add(ticket);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return tickets;
}
private Ticket cursorToTicket(Cursor cursor) {
Ticket ticket = new Ticket();
ticket.setId(cursor.getLong(0));
ticket.setTicketId(cursor.getLong(1));
ticket.setTicketTitle(cursor.getString(2));
ticket.setClientName(cursor.getString(3));
ticket.setProjectName(cursor.getString(4));
ticket.setTicketDescription(cursor.getString(5));
return ticket;
}
}
在我的活动中,我首先通过定义它来调用TicketsDataSource类:
private TicketsDataSource datasource;
然后从if语句中调用它,检查服务器是否已成功调用:
else {
datasource = new TicketsDataSource(this);
datasource.open();
}
不幸的是,当我切换到DDMS视图时,在多次运行应用程序后,我看不到任何正在创建的数据库。事实上,我甚至无法在DDMS视图的主“data”文件夹中看到任何其他文件夹。我没有得到任何错误消息(除了'eglSurfaceAttrib未实现',这在谷歌搜索后似乎无关紧要),但是当我在MySQLiteHelper类的onCreate函数中放置系统打印时,它不会在日志中打印任何内容(它确实出现在else语句中)。我不确定我的Java代码是否有问题,或者我的常规设置是否有问题。
先谢谢你的帮助, 丹尼斯
答案 0 :(得分:0)
如果您是,您将只能看到/ data文件夹中的文件/文件夹 a:使用模拟器,或 b:使用root电话。这些是受保护的文件夹,不能被凡人看到。
对你所写的内容的真正考验是这样的 - 你能写入数据库并从数据库中读取吗?
答案 1 :(得分:0)
对我来说最好的android数据库故障排除解决方案是将此方法添加到我的SQliteHelper类中,该类将sqlite数据库复制到您测试的设备上的SD卡。然后,您可以使用任意数量的sqlite manager桌面应用程序打开它,并验证您的表和数据。您不需要以这种方式root手机。在开发过程中,我坚持调用这个方法,我需要获取db的快照:
public static void backup() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//com.example//databases//" + DATABASE_NAME;
String backupDBPath = DATABASE_VERSION + DATABASE_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, "//Download//" + backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
您需要为清单添加外部存储权限。