我有一个在资产中导入的试用数据库,它是预先填充的.它有6列:_ID,QUESTION,ANSWER,OPTION1,OPTION2,OPTION3,我现在有10行.我正在做类似测验的事情.我想要的是当我打开测验活动以读取一个随机行,然后将setText设置为我创建的4个按钮,button1 = ANSWER,button2 = OPTION1,button3 = OPTION2,button = OPTION3,再次随机.我知道如何将文本设置为按钮和所有,我只想知道如何从数据库中读取所有内容.我有导入数据库的DataBaseHelper类:
package rs.androidaplikacijekvizopstekulture;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import rs.androidaplikacijekvizopstekulture.Pitanja;
public class DataBaseHelper extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME ="pitanja";// Database name
private static final int DATABASE_VERSION …Run Code Online (Sandbox Code Playgroud) 我有片段,我在每个片段中缓存数据.我通过从服务器下载doInBackground方法将数据缓存在AsyncTask中,并将其保存在onPostExecute中保存到我的数据库中.所以我总是在onPostExecution中打开数据库连接.如果我滚动"快速"扔掉片段,我认为AsyncTasks通过了预览实例,并且android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)在行中会有一个,我在那里打开连接(获取dbHelper.getWritableDatabase())
我认为有两种方法可以解决这个问题:
onPostExecution方法必须等到预览AsyncTask(onPostExecution)完成
有一种方法可以为AsyncTask的所有实例使用相同的数据库连接,而无需相互锁定它们
它是否正确?有人有点想法,怎么做?
这是架构:

SQL查询是:SELECT*FROM unjdat其中COL_1 = " myWord ";
即,我想显示col_1为myWord的行的所有列.
int i;
String temp;
words = new ArrayList<String>();
Cursor wordsCursor = database.rawQuery("select * from unjdat where col_1 = \"apple\" ", null); //myWord is "apple" here
if (wordsCursor != null)
wordsCursor.moveToFirst();
if (!wordsCursor.isAfterLast()) {
do {
for (i = 0; i < 11; i++) {
temp = wordsCursor.getString(i);
words.add(temp);
}
} while (wordsCursor.moveToNext());
}
words.close();
Run Code Online (Sandbox Code Playgroud)
我认为问题在于循环.如果我删除for循环并执行wordsCursor.getString(0)它的工作.如何循环获取所有列?
注意:
我在android SQLite中有一个表,我试图弄清楚如何重置KEY_ID,我读到:DELETE FROM tablename应该删除所有内容并将自动增量字段重置为0但是当我这样做时它只是删除数据.插入新记录后,自动增量会在删除之前从中断处继续.我已经实现了一个longitemclicklistner列表,它获取了位置,我想从列表中删除特定项目.这是我的代码
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
getWritableDatabase().delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) } );
db.close();
}
Run Code Online (Sandbox Code Playgroud)
这是代码 onlongclicklistner
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos"+" "+pos);
final int p=pos+1;
final AlertDialog.Builder b = new AlertDialog.Builder(MainActivity.this);
b.setIcon(android.R.drawable.ic_dialog_alert);
b.setMessage("Are you sure you want to DELETE this Entry?");
b.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface …Run Code Online (Sandbox Code Playgroud) 我相信错误在于我设置DATABASE_CREATE的方式.我在这里错过了什么吗?我得到SQLiteException,其中long id = -1.如果我提供足够的细节,请告诉我.我试过设置KEY_ID ="_ id";
E/SQLiteLog(2385): (1) near "group": syntax error
E/SQLiteDatabase(2385): Error inserting group=demo
E/SQLiteDatabase(2385): android.database.sqlite.SQLiteException: near "group": syntax error (code 1): , while compiling: INSERT INTO groups(group) VALUES (?)
LOG(2385): Inserting record...
LOG(2385): mGroupName = demo
LOG(2385): long id = -1
Run Code Online (Sandbox Code Playgroud)
DBAdapter.java
public static final String KEY_ID = "id";
public static final String KEY_GROUP_NAME = "group";
public static final String TAG = "DBAdapter";
public static final String DATABASE_NAME = "GroupsDB";
public static final String DATABASE_TABLE = "groups"; …Run Code Online (Sandbox Code Playgroud) 我想循环遍历sqlite数据库if if(johnny)是否存在于数据库中,但我的代码不起作用
if(!cursor.isAfterLast())
{
do
{
for(int i = 0; i<cursor.getCount(); i++)
{
if("johnny" == cursor.getString(1))
{
Toast.makeText(getApplicationContext(), "string found!!! =)", Toast.LENGTH_LONG).show();
}
}
}
while (cursor.moveToNext());
}
cursor.close();
}
Run Code Online (Sandbox Code Playgroud) 给定像这样wort的表中的列中的条目woerter
henlo
oenlh
olneh
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个sqlite命令,列出所有word包含的条目h e l o(例如,实际上这些字母总是不同) - 按此顺序.所以在这个例子中结果应该是
henlo
因为有
heñlo
换句话说:选择字符串中某处的所有条目h,之后有一个e,l和o.它不需要在它之后,而是在它之后的某个地方.例如:
ssssshssssssessssssslsssossssso
会在结果中,
ssosssssossssshssssssesssssssls
不会.
到目前为止我的方法:
SELECT*FROM woerter WHERE wort LIKE'h%'AND wort LIKE'%e%'AND wort LIKE'%l%'AND wort LIKE'%o'
这是有缺陷的,因为它没有正确地整合e和l的顺序,它需要h和o在开头和结尾.
我之前已经问过这个问题并且没有得到任何适当的答案.
我在Play商店发布了一个应用程序,它大量使用SQLite数据库.
现在我需要使用SQLCipher来保护数据库.这是我面临的一些问题.
1)如何将SQLCipher与我现有的未加密数据库无缝集成,以便我的应用程序正常工作但现在数据库已加密?
(我想要一个简短的教程)
2)我应该如何检查数据库是否未加密,然后如何加密?我应该只做一次吗?什么应该是最好的做法?
(可能的副本不回答这个)
3)当我加密现有的未加密数据库时,SQLCipher是否创建了一个新数据库?如果是的话,我应该如何管理这个新的?那个未加密的旧数据库怎么样?它还留在那里吗?
(重复问题中也没有提供此解释)
我正在制作一个小应用程序,以读取一些txt文件并将其导入设备上的SQLite数据库。
问题是,当我正确读取和导入文件时,应用在执行该方法时会冻结。我想显示进度条或类似的内容,但是由于这个问题,我什么也做不了。
这是导入方法代码:
private void importFilesFromTxt() {
bd = helper.getWritableDatabase();
File directorio = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);
File file = new File(directorio, "art01.txt");
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
String[] parts;
String sql = "delete from Articulos";
bd.execSQL(sql);
while ((line = br.readLine()) != null) {
if (line.length() != 328) {
parts = line.split("\\#+");
Articulo arti = new Articulo(Integer.parseInt(parts[0]), parts[1], quitarEspaciosInt(parts[2]), quitarEspaciosInt(parts[3])
, convertirDecimal(parts[4]), quitarEspaciosInt(parts[5]), quitarEspaciosInt(parts[6]), quitarEspaciosFloat(parts[7]), quitarEspaciosInt(parts[8]));
helper.addArticulo(arti);
}
}
bd.close();
br.close();
} catch (IOException e) {
System.out.println(e.toString());
} …Run Code Online (Sandbox Code Playgroud) 我sqlite用这种形式写了句子:
String SQL_CREATE_BACKUP ="CREATE TABLE " +smsEntry.BACKUP +"("
+smsEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+smsEntry.BACKUP_LAST_MODE +" INTEGER NOT NULL,"
+smsEntry.BACKUP_CURRENT_MODE +" INTEGER NOT NULL,"
+smsEntry.BACKUP_LAST_TYPE +" INTEGER NOT NULL,"
+smsEntry.BACKUP_CURRENT_TYPE +" INTEGER NOT NULL,"
+smsEntry.BACKUP_MILLI_DATE +" LONG NOT NULL,"
+smsEntry.BACKUP_CURRENT_DATE + " TEXT NOT NULL);";
db.execSQL(SQL_CREATE_BACKUP);
Run Code Online (Sandbox Code Playgroud)
但是,当我想运行此代码时,我收到此错误消息:
<column definition name> or <table constraint>expected,got 'current_date'
Run Code Online (Sandbox Code Playgroud)
我该如何修复这个错误?
Thankes.