当前位置:网站首页>Optimize database queries using the cursor object of SQLite
Optimize database queries using the cursor object of SQLite
2022-07-05 09:47:00 【Black Mountain demon 2018】
Use a background
I read a piece of code written by my colleagues very early , very interesting , In order to obtain the id aggregate , Through the first dao Object to get all personnel information in the database ,
Then one by one add Into the list aggregate . If the number of database reaches tens of thousands , In fact, whether in terms of memory or efficiency , This way is not advisable .
/**
* Get personId aggregate
* todo This method can be optimized
*
* @return
*/
public List<String> getPersonIdList() {
List<PersonInfo> personInfos = queryAll();
if (personInfos == null || personInfos.size() == 0) {
return new ArrayList<>();
}
List<String> personIdsList = new ArrayList<>();
for (PersonInfo info : personInfos) {
if (info == null || TextUtils.isEmpty(info.getPersonId())) {
continue;
}
personIdsList.add(info.getPersonId());
}
return personIdsList;
}
Cursor object
Let's get to know Cursor object , He is right sqlite The result set after database query , It can be understood as a set of each row of query results .
Simple api Use as follows
// Close cursor , Release resources
close()
// Retrieve the text of the requested column in the buffer , Store it
copyStringToBuffer(int columnIndex, CharArrayBuffer buffer)
// Returns the total number of all columns
getColumnCount()
// Returns the name of the specified column , Returns if none exists -1
getColumnIndex(String columnName)
// Returns the specified column name from zero , If it does not exist, it will throw IllegalArgumentException abnormal .
getColumnIndexOrThrow(String columnName)
// Returns the column name from the given index
getColumnName(int columnIndex)
// Returns the column name of a string array
getColumnNames()
// return Cursor The number of lines in
getCount()
// Move the cursor to the first line
moveToFirst()
// Move the cursor to the last line
moveToLast()
// Move the cursor to the next line
moveToNext()
// Move the cursor to an absolute position
moveToPosition(int position)
Move the cursor to the previous line
moveToPrevious()
Use Cursor Optimize database queries
/**
* Get personId aggregate
*
* @return
*/
public List<String> getPersonIdList() {
List<String> personIdsList = new ArrayList<>();
Cursor cursor = null;
try {
cursor = DbCore.getDaoSession().getRecordDao().getDatabase().rawQuery("select PERSON_ID from person_info", null);
while (cursor.moveToNext()) {
personIdsList.add(cursor.getString(0));
}
cursor.close();
} catch (Exception e) {
if (cursor != null) {
cursor.close();
cursor = null;
}
}
return personIdsList;
}
The above code can be seen through sql Statement to query only the required fields PERSON_ID Information , Then the data of each row passes cursor.getString(0) To get , In terms of memory allocation , This avoids building a large number of unnecessary personInfos object , In terms of reading efficiency , Also avoid querying unnecessary information .
边栏推荐
- uni-app---uni.navigateTo跳转传参使用
- Tdengine can read and write through dataX, a data synchronization tool
- 植物大战僵尸Scratch
- uni-app---uni. Navigateto jump parameter use
- Community group buying exploded overnight. How should this new model of e-commerce operate?
- 从“化学家”到开发者,从甲骨文到 TDengine,我人生的两次重要抉择
- Officially launched! Tdengine plug-in enters the official website of grafana
- 揭秘百度智能测试在测试自动执行领域实践
- 单片机原理与接口技术(ESP8266/ESP32)机器人类草稿
- Android SQLite database encryption
猜你喜欢
How to empty uploaded attachments with components encapsulated by El upload
Principle and performance analysis of lepton lossless compression
The research trend of map based comparative learning (gnn+cl) in the top paper
OpenGL - Model Loading
为什么不建议你用 MongoDB 这类产品替代时序数据库?
Tdengine already supports the industrial Intel edge insight package
[technical live broadcast] how to rewrite tdengine code from 0 to 1 with vscode
LeetCode 503. Next bigger Element II
初识结构体
Unity skframework framework (XXIII), minimap small map tool
随机推荐
About getfragmentmanager () and getchildfragmentmanager ()
E-commerce apps are becoming more and more popular. What are the advantages of being an app?
What are the advantages of the live teaching system to improve learning quickly?
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
OpenGL - Coordinate Systems
Design and exploration of Baidu comment Center
Three-level distribution is becoming more and more popular. How should businesses choose the appropriate three-level distribution system?
基于宽表的数据建模应用
Data visualization platform based on template configuration
Project practice | excel export function
Solve liquibase – waiting for changelog lock Cause database deadlock
[app packaging error] to proceed, either fix the issues identified by lint, or modify your build script as follow
How Windows bat script automatically executes sqlcipher command
LeetCode 31. Next spread
Roll up, break 35 - year - old Anxiety, animation Demonstration CPU recording Function call Process
TDengine × Intel edge insight software package accelerates the digital transformation of traditional industries
H. 265 introduction to coding principles
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article
Unity skframework framework (24), avatar controller third person control
代码语言的魅力