Answer the question
In order to leave comments, you need to log in
How to fix error when using SQLite in kotlin?
Now I am writing a program for calculating molecular weights in Kotlin, and I decided to use SQLite to create a database with element names and their properties, but when I ran my code on a device, the application crashed with this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.listochek.chem.calc, PID: 3941
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.listochek.chem.calc/com.listochek.chem.calc.MainActivity}: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE IF NOT EXISTS `Elements`(id INTEGER PRIMARY KEY UNIQUE AUTOINCREMENT, Химический элемент TEXT, Символ TEXT, Атомная масса REAL);
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE IF NOT EXISTS `Elements`(id INTEGER PRIMARY KEY UNIQUE AUTOINCREMENT, Химический элемент TEXT, Символ TEXT, Атомная масса REAL);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1770)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1698)
at org.jetbrains.anko.db.DatabaseKt.createTable(Database.kt:80)
at com.listochek.chem.calc.Data.DBHelper.onCreate(DBHelper.kt:20)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:393)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
at org.jetbrains.anko.db.ManagedSQLiteOpenHelper.openDatabase(Database.kt:186)
at org.jetbrains.anko.db.ManagedSQLiteOpenHelper.use(Database.kt:177)
at com.listochek.chem.calc.MainActivity.onCreate(MainActivity.kt:20)
at android.app.Activity.performCreate(Activity.java:7224)
at android.app.Activity.performCreate(Activity.java:7213)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
database.use {
insert(Elements.TABLE_NAME,
Elements.COLUMN_ID to 1,
Elements.COLUMN_ELEMENT to "Водород",
Elements.COLUMN_SYMBOL to "H",
Elements.COLUMN_MASS to 1.00794)
insert(Elements.TABLE_NAME,
Elements.COLUMN_ID to 2,
Elements.COLUMN_ELEMENT to "Гелий",
Elements.COLUMN_SYMBOL to "He",
Elements.COLUMN_MASS to 4.00260)
}
val test = database.use {
select(Elements.TABLE_NAME).exec { parseList<Any>(classParser()) }
}
ListView.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, test)
}
}
data class Elements(var id : Int, var Element : String, var Symbol : String, var Mass : Double) {
companion object {
const val TABLE_NAME = "Elements"
const val COLUMN_ID = "id"
const val COLUMN_ELEMENT = "Element"
const val COLUMN_SYMBOL = "Symbol"
const val COLUMN_MASS = ""
}
}
код DBHelper:
class DBHelper(ctx: Context) : ManagedSQLiteOpenHelper(ctx, "Elements", null, 1) {
companion object {
private var instance: DBHelper? = null
@Synchronized
fun getInstance(ctx: Context): DBHelper {
if (instance == null) {
instance = DBHelper(ctx.getApplicationContext())
}
return instance!!
}
}
override fun onCreate(db: SQLiteDatabase) {
db.createTable("Elements", true,
"id" to INTEGER + PRIMARY_KEY + UNIQUE + AUTOINCREMENT,
"Химический элемент" to TEXT,
"Символ" to TEXT,
"Атомная масса" to REAL
)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.dropTable("Elements", true)
}
}
val Context.database: DBHelper get() = DBHelper.getInstance(applicationContext)
Answer the question
In order to leave comments, you need to log in
It is written that the error is in the sqlite syntax. At first glance, there is a whole set of errors, read at least the sqlite help and write the correct query.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question