I want to create table for selecting file, title and date. after clicking the save button I am getting this error. The error in my program is this:-
2022-03-22 12:09:06.679 7545-7545/com.example.ca E/SQLiteLog: (1) incomplete input in "CREATE TABLE FILES_TABLE(_id INTEGER PRIMARY KEY,title TEXT, date TEXT, filepath TEXT, filename TEXT"
2022-03-22 12:09:06.681 7545-7545/com.example.ca D/AndroidRuntime: Shutting down VM
2022-03-22 12:09:06.685 7545-7545/com.example.ca E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ca, PID: 7545
android.database.sqlite.SQLiteException: incomplete input (code 1 SQLITE_ERROR[1]): , while compiling: CREATE TABLE FILES_TABLE(_id INTEGER PRIMARY KEY,title TEXT, date TEXT, filepath TEXT, filename TEXT
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1463)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:901)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:62)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2437)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:2359)
at com.example.ca.database.FilesDatabase.onCreate(FilesDatabase.kt:31)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:482)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:387)
at com.example.ca.database.FilesDatabase.addAuditDatabase(FilesDatabase.kt:40)
at com.example.ca.Datafunction.onCreate$lambda-1(Datafunction.kt:56)
at com.example.ca.Datafunction.$r8$lambda$GgvsgvkXlLceSKUS2Jr-7Wv4XgM(Unknown Source:0)
at com.example.ca.Datafunction$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:8160)
at android.widget.TextView.performClick(TextView.java:16222)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
2022-03-22 12:09:06.724 7545-7545/com.example.ca I/Process: Sending signal. PID: 7545 SIG: 9
here is my datafunction activity code in which I am trying to save using button.setonClickListner title, date, and files path :-
class Datafunction : AppCompatActivity() {
private var cal = Calendar.getInstance()
private var filepath: String? = null
private var namedata: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_datafunction)
val myFormat = "dd/MM/yyyy" // mention the format you need
val sdf = SimpleDateFormat(myFormat, Locale.getDefault()) // A date format
file_date.setText(sdf.format(cal.time)?.toString())
btn_select_file.setOnClickListener {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.setType("*/*")
startActivityForResult(intent, 1)
}
save.setOnClickListener {
when {
file_title.text.isNullOrEmpty() -> {
Toast.makeText(this, "Please enter title", Toast.LENGTH_SHORT).show()
}
file_name.text.isNullOrEmpty() -> {
Toast.makeText(this, "Please add file.", Toast.LENGTH_SHORT)
.show()
}
else -> {
val filedata = FilesData(
0,
file_title.text.toString(),
file_date.text.toString(),
filepath!!,
namedata!!
)
val filedbhandler = FilesDatabase(this, "Files_Database")
val addfiledata = filedbhandler.addFilesdatabase(filedata)
if (addfiledata > 0){
Toast.makeText(this, "Details saved successfully", Toast.LENGTH_SHORT).show()
finish()
}
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
val imagedata = data?.data
val name = imagedata.toString()
namedata = name.substringAfterLast("/")
filepath = imagedata!!.path
file_name.text = filepath
file_name.visibility = View.VISIBLE
}
}
}
}
here is my model class code:-
data class FilesData(
val id: Int,
val title: String,
val date: String,
val filepath: String,
val fileName: String
) : Serializable
here is my database handler code:-
class FilesDatabase(context: Context, SQLname: String) :
SQLiteOpenHelper(context, SQLname, null, DATABASE_VERSION) {
companion object{
private const val DATABASE_VERSION = 2
private const val TABLE_FILES_DATABASE ="FILES_TABLE"
private const val KEY_ID = "_id"
private const val KEY_TITLE = "title"
private const val KEY_DATE = "date"
private const val KEY_FILE_PATH = "filepath"
private const val KEY_FILE_NAME = "filename"
}
override fun onCreate(db: SQLiteDatabase?) {
val CREATE_TABLE = ( "CREATE TABLE " TABLE_FILES_DATABASE "("
KEY_ID " INTEGER PRIMARY KEY,"
KEY_TITLE " TEXT, "
KEY_DATE " TEXT, "
KEY_FILE_PATH " TEXT, "
KEY_FILE_NAME " TEXT")
db?.execSQL(CREATE_TABLE)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db!!.execSQL("DROP TABLE IF EXISTS $TABLE_FILES_DATABASE")
onCreate(db)
}
fun addFilesdatabase(filesdata: FilesData): Long {
val db = this.writableDatabase
val contentValues = ContentValues()
contentValues.put(KEY_TITLE, filesdata.title)
contentValues.put(KEY_DATE, filesdata.date)
contentValues.put(KEY_FILE_PATH, filesdata.filepath)
contentValues.put(KEY_FILE_NAME, filesdata.fileName)
// Inserting Row
val result = db.insert(TABLE_FILES_DATABASE, null, contentValues)
//2nd argument is String containing nullColumnHack
db.close() // Closing database connection
return result
}
}
I am unable to find what is wrong in this code. I am unable to find the error. Can someone please help me?
CodePudding user response:
It looks like you forgot a ')' at the end of your CREATE TABLE:
override fun onCreate(db: SQLiteDatabase?) {
val CREATE_TABLE = ( "CREATE TABLE " TABLE_FILES_DATABASE "("
KEY_ID " INTEGER PRIMARY KEY,"
KEY_TITLE " TEXT, "
KEY_DATE " TEXT, "
KEY_FILE_PATH " TEXT, "
KEY_FILE_NAME " TEXT);")
db?.execSQL(CREATE_TABLE)
}