我正在创建一个基于 Android 的笔记应用程序。我想在我的应用程序中向用户显示注释的大小。我知道文件和图像的大小可以在 sqlite 中轻松确定,但在这里我想获取标题和描述列占用的大小或内存,这些列是 TEXT 和 VARCHAR 数据类型(以字节为单位)。在我的案例中,数据库表中的一行代表一个注释。
我已经尝试了“Length”和“MAX”函数的所有可能的解决方案,但没有成功。 我已为此目的使用了准备好的 SQLite 语句,但无法获取具有特定 ID 的列的大小。 这是我的代码:
public class DataAdapterText
{
Context c;
SQLiteDatabase db;
DBHelperText helper;
public DataAdapterText(Context c) {
this.c = c;
helper=new DBHelperText(c);
}
public void getSize(int id)
{
SQLiteStatement byteStatement = db.compileStatement("SELECT LENGTH(txtitle) FROM TX_Table WHERE tid='"+id+"'");
long bytes = byteStatement.simpleQueryForLong();
long kilobytes = 1024,
megabytes = kilobytes * kilobytes,
gigabytes = megabytes * kilobytes,
terabytes = gigabytes * kilobytes;
if (bytes<kilobytes)
{
Toast.makeText(c, bytes+" B", Toast.LENGTH_SHORT).show();
}
else if (bytes>=kilobytes && bytes<megabytes)
{
Toast.makeText(c, bytes+" KB", Toast.LENGTH_SHORT).show();
}
else if (bytes>=megabytes && bytes<gigabytes)
{
Toast.makeText(c, bytes+" MB", Toast.LENGTH_SHORT).show();
}
else if (bytes>=gigabytes && bytes<terabytes)
{
Toast.makeText(c, bytes+" GB", Toast.LENGTH_SHORT).show();
}
}
}
public class TextNotes extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_text_notes);
DataAdapterText dataAdapterText=new DataAdapterText(TextNotes.this);
dataAdapterText.openDB();
dataAdapterText.getSize(Id);//here Id is the ID of specific item when clicked by the user and is coming from the Recycler adapter. No issue with this ID!
dataAdapterText.closeDB();
}
如果上面的 SELECT 语句中没有 WHERE 子句,则此代码运行时不会出错。但由于我想要具有特定 ID 的列的长度,所以我需要 WHERE 子句,但它给出了 WHERE 子句的错误。 如果有其他方法可以解决这个问题并获取笔记的大小,请指导我。 这是 logcat 错误:
2019-05-18 14:19:25.893 13286-13286/com.example.unifiednotesnew E/asset: AssetManager::addSystemOverlays delete oidmap
2019-05-18 14:19:39.943 13286-13286/com.example.unifiednotesnew E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.unifiednotesnew, PID: 13286
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.unifiednotesnew/com.example.unifiednotesnew.TextNotes}: android.database.sqlite.SQLiteDoneException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2750)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: android.database.sqlite.SQLiteDoneException
at android.database.sqlite.SQLiteConnection.nativeExecuteForLong(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLong(SQLiteConnection.java:604)
at android.database.sqlite.SQLiteSession.executeForLong(SQLiteSession.java:790)
at android.database.sqlite.SQLiteStatement.simpleQueryForLong(SQLiteStatement.java:107)
at com.example.unifiednotesnew.Adapter.DataAdapterText.getSize(DataAdapterText.java:193)
at com.example.unifiednotesnew.TextNotes.onCreate(TextNotes.java:125)
at android.app.Activity.performCreate(Activity.java:6757)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2703)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2811)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6316)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
最佳答案
问题
您的问题是没有选择任何行,并且没有可返回的值。
那就是:-
An exception that indicates that the SQLite program is done. Thrown when an operation that expects a row (such as SQLiteStatement#simpleQueryForString or SQLiteStatement#simpleQueryForLong) does not get one.
修复
您可以捕获异常,例如
........
SQLiteStatement byteStatement = db.compileStatement("SELECT LENGTH(txtitle) FROM TX_Table WHERE tid='"+id+"'");
long bytes = 0;
try {
bytes = byteStatement.simpleQueryForLong();
} catch (SQLiteDoneException e) {
}
long kilobytes = 1024,
........
或通过游标获取值,例如类似的东西:-
public long getColumnLengthById(String table, String column, long id, String idcolumn) {
long rv = 0;
String rv_column = "LEN";
String[] columns = new String[]{"length(" + column + ") AS " + rv_column};
String whereclause = idcolumn + "=?";
String[] whereargs = new String[]{String.valueOf(id)};
SQLiteDatabase db = this.getWritableDatabase();
Cursor csr = db.query(table,columns,whereclause,whereargs,null,null,null);
while (csr.moveToNext()) {
rv = rv + csr.getLong(csr.getColumnIndex(rv_column));
}
csr.close();
return rv;
}
关于java - 如何在Android中的SQLite数据库中查找TEXT或VARCHAR类型的特定列占用的内存(大小)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56197753/