如何从手机查看 SQLite 数据(应用需要蓝牙)
Posted
技术标签:
【中文标题】如何从手机查看 SQLite 数据(应用需要蓝牙)【英文标题】:How to view SQLite data from Phone (app requires bluetooth) 【发布时间】:2014-07-14 16:26:57 【问题描述】:我一直在尝试使用 DDMS 方法和 SQLite 管理器等查看与我的应用关联的 SQlite 数据库。
但是,我无法使用模拟器查看数据,因为我的应用需要蓝牙并且不受模拟器支持
如何在我的应用程序中查看数据库,而不是使用模拟器?
编辑,创建数据库的当前代码:
package com.example.multapply;
public class DatabaseHelper extends SQLiteOpenHelper
// Database Version
private static final int DATABASE_VERSION = 3;
// Database Name
private static final String DATABASE_NAME = "MultapplyDatabase";
// Contacts table name
private static final String TABLE_SCORE = "scores";
// Contacts Table Columns names
private static final String COL_NAME = "name";
private static final String COL_SCORE = "score";
private static final String COL_DATE = "date";
/**
* Constructor
* @param context
*/
public DatabaseHelper(Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);
/**
* Method that creates the database
*/
@Override
public void onCreate(SQLiteDatabase db)
//NOTE: may need to alter the below to take out everything after INTEGER
String CREATE_TABLE_SCORE = "CREATE TABLE " + TABLE_SCORE + "("
+ COL_NAME + " STRING PRIMARY KEY," + COL_SCORE + " INTEGER," + COL_DATE + " LONG" + ")";
db.execSQL(CREATE_TABLE_SCORE);
添加到数据库:
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addScore(new Score(UserName.getUserName(), score, System.currentTimeMillis() ));
编辑(出现以下错误):
07-14 20:10:07.970: E/mypck(12270): /data/data/com.example.multapply/databases/MultapplyDatabase.db: open failed: ENOENT (No such file or directory)
07-14 20:10:07.970: E/mypck(12270): java.io.FileNotFoundException: /data/data/com.example.multapply/databases/MultapplyDatabase.db: open failed: ENOENT (No such file or directory)
07-14 20:10:07.970: E/mypck(12270): at libcore.io.IoBridge.open(IoBridge.java:409)
07-14 20:10:07.970: E/mypck(12270): at java.io.FileInputStream.<init>(FileInputStream.java:78)
07-14 20:10:07.970: E/mypck(12270): at com.example.multapply.ExportDatabaseFileTask.copyFile(ExportDatabaseFileTask.java:71)
07-14 20:10:07.970: E/mypck(12270): at com.example.multapply.ExportDatabaseFileTask.doInBackground(ExportDatabaseFileTask.java:49)
错误 2:
07-14 20:17:15.026:E/DatabaseUtils(814):将异常写入包裹 07-14 20:17:15.026: E/DatabaseUtils(814): java.lang.SecurityException: Permission Denial: get/set setting for user asks to run as user -2 but is call from user 0;这需要 android.permission.INTERACT_ACROSS_USERS_FULL 07-14 20:17:15.026: E/DatabaseUtils(814): at com.android.server.am.ActivityManagerService.handleIncomingUser(ActivityManagerService.java:14608)
注意:我已经将 android.permission.INTERACT_ACROSS_USERS_FULL 添加到清单中,但仍然收到错误消息。
编辑 3(全班):
package com.example.multapply;
//Importing resources
import java.util.Date;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Class holding the activity that has the 10 random sums for the user to answer
* @author Ross
*
*/
public class RandomTest extends Activity implements View.OnClickListener
// declare vars
TextView text;
EditText answer;
Button submit;
int random1;
int random2;
String[] question = new String[10];
int correctAnswer[] = new int[10];
int[] results = new int[10];
int score = 0;
int questionNumber = 1;
MediaPlayer correctNoise;
MediaPlayer incorrectNoise;
ImageView imageRandom;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
// initialising variables
initialiseVars();
// set up random
setUpRandom();
// Set text view equal to question in array
text.setText(question[questionNumber - 1]);
// set on click listener for the submit button
submit.setOnClickListener(this);
// updateQuestion
updateQuestion();
/**
* Method that initialises variables
*/
public void initialiseVars()
correctNoise = MediaPlayer.create(RandomTest.this, R.raw.correctnoise);
incorrectNoise = MediaPlayer.create(RandomTest.this, R.raw.incorrectnoise);
text = (TextView) findViewById(R.id.tvTopRandomTest);
answer = (EditText) findViewById(R.id.etEnterAnswerRandomTest);
submit = (Button) findViewById(R.id.btnSubmitRandomTest);
imageRandom= (ImageView) findViewById(R.id.imageViewRandomTest);
/**
* Method that creates the random sum for user to answer
*/
public void setUpRandom()
// setting up new random
Random random = new Random();
// Generating random number between 1 and 12
random1 = random.nextInt(12) + 1;
// Generating another random number between 1 and 12
random2 = random.nextInt(12) + 1;
// Creating random question String
question[questionNumber - 1] = random1 + " x " + random2 + " = ";
// Creating correct answer to question
correctAnswer[questionNumber - 1] = random1 * random2;
/**
* Method that updates question after each click
*/
public void updateQuestion()
// updating question after each click
setUpRandom();
text.setText(question[questionNumber - 1]);
answer.setText("");
public void onClick(View v)
// sets text view equal to what is entered in editText
final String entry = answer.getText().toString();
// convert from string value to int
int a = Integer.parseInt(entry); //
// setting the user answer equal to the correct part of results array
results[questionNumber - 1] = a;
// If user answer is equal to correct answer then increase score
if (a == correctAnswer[questionNumber - 1])
score++;
correctNoise.start();
imageRandom.setImageResource(R.drawable.thumbsup);
else
incorrectNoise.start();
imageRandom.setImageResource(R.drawable.thumbsdown);
// if question number is under 10
if (questionNumber < 10)
// updates question number
questionNumber++;
// called after an answer is given
updateQuestion();
else
//Attempting to add the score to the database from here
DatabaseHelper db = new DatabaseHelper(this);
// Passing values to the results activity
Intent intent = new Intent(this, RandomTestResults.class);
intent.putExtra("results", results);
intent.putExtra("Questions", question);
intent.putExtra("CorrectAnswer", correctAnswer);
intent.putExtra("score", score);
// Start Activity
this.startActivity(intent);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addScore(new Score(UserName.getUserName(), score, System.currentTimeMillis() ));
//attempting to export the file to the sd card
ExportDatabaseFileTask task = new ExportDatabaseFileTask();
task.execute();
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Score> scores = db.getAllScores();
for (Score s : scores)
String log = "Name: " + s.getName() + " ,Score: " + s.getScore() + "Date: " + s.getDate();
// Writing Contacts to log
Log.d("Name: ", log);
【问题讨论】:
【参考方案1】:我知道两种可能的方法:
1 - 使用您自己的应用程序将 db 文件的副本复制到 sdcard 中,它是可见的。
//Get your database file
File database = context.getDatabasePath("dbName.db");
//Get your sdcard location
File extStore = Environment.getExternalStorageDirectory();
//Copy sample
file = new File(extStore, "dbName.db");
file.createNewFile();
FileChannel inChannel = new FileInputStream(database).getChannel();
FileChannel outChannel = new FileOutputStream(file).getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
2 - 根您的设备并授予您数据库的整个路径的权限,这样您甚至可以通过 Eclipse 拉取它。
chmod 777 all paths your need, ussualy /data/data/your.package/database recusivally
【讨论】:
你能看看我对这个问题的编辑吗?我不确定如何用我当前的代码实现你的建议。谢谢 这段代码实际上与帮助类无关..但类似于“导出”事件。 我正在寻找的解决方案是否与此类似? ***.com/questions/2814213/… 是的,这正是我给出的第一个选项,但是有点脏,你构建它有困难吗? 是的,我需要创建一个类的实例才能使用它吗?我有点迷茫【参考方案2】:将SQLiteOpenHelper
的数据库重命名为/mnt/sdcard/myapp.db
。
然后您将在那里创建一个数据库,也可以从其他地方访问它
【讨论】:
我不确定你的意思,请查看我的编辑,因为其中包含我创建和命名数据库的代码! 你指的是这种解决方案吗? ***.com/questions/2814213/… 是的,将 DATABASE_NAME 更改为我所说的,不要忘记写入外部存储的权限 那么我需要实现我将链接发送到的代码(另一个问题)还是只需将当前答案中的代码更改为 /mnt/sdcard/myapp.db ? 是的,然后完全重新安装以上是关于如何从手机查看 SQLite 数据(应用需要蓝牙)的主要内容,如果未能解决你的问题,请参考以下文章
在 android 中将 SQLite 数据从平板电脑更新到手机