我的环境:Qt5.15.2 + android10
1.使用Qtcreator新建一个android工程。pro文件如下:
QT += quick sql qml
CONFIG += c++11
android {
QT += androidextras
}
CONFIG += c++11
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
databasesevice.cpp \
main.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DISTFILES += \
android/AndroidManifest.xml \
android/build.gradle \
android/gradle.properties \
android/gradle/wrapper/gradle-wrapper.jar \
android/gradle/wrapper/gradle-wrapper.properties \
android/gradlew \
android/gradlew.bat \
android/res/values/libs.xml
#注意这句,会把android目录下的资源打包
ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android
HEADERS += \
databasesevice.h
目录结构如下:
2.在android目录中,新建目录assets,并把数据库文件放入该目录。
如下:
编译打包,然后运行到android设备上。
3.在android设备目录上查找该数据库文件如下:
读取数据库结果如下: