SQLite-数据库库-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

SQLite-数据库库

Puppet 2020-01-12 11:08:45 134

您可以通过解决缺少的列来解决问题吗?在我看来,我所做的一切都正确,但是我得到一个错误,那就是一栏有问题。

错误

E / SQLiteLog:(1)没有此类列:Stezenie D / AndroidRuntime:关闭VM E / AndroidRuntime:致命例外:main进程:com.example.apkadlapacjenta,PID:11261 java.lang.RuntimeException:无法启动活动ComponentInfo { com.example.apkadlapacjenta / com.example.apkadlapacjenta.HistoriaPomiarow}:android.database.sqlite.SQLiteException:无此类列:Stezenie(代码1 SQLITE_ERROR):,同时编译:SELECT Id,Stezenie,Zegar,Data,Posilek,Stres ,沙门菌,高血脂症 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)在android。 android上的app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2016) .os.Handler.dispatchMessage(Handler.java:107)在android.os.Looper.loop(Looper.java:214)在android.app.ActivityThread.main(ActivityThread.java:7356)在java.lang.reflect。 com.android.internal.os上的Method.invoke(本机方法)。com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)上的RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)原因:android.database.sqlite.SQLiteException:无此类列:Stezenie(代码1 SQLITE_ERROR):,而在编译时:SELECT Id,Stezenie,Zegar,Data,Posilek,Stres,Samopoczucie,Hiperglikemia FROM Pomiary位于android.database.sqlite.SQLiteConnection.nativePrepareStatement(本地方法),位于android.database.sqlite.SQLiteConnection.acquirePreparedStatement( (android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)的android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)的android.database.sqlite.SQLiteProgram。( SQLiteProgram.java:61),位于android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37),位于android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46),位于android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1443),位于android.database.sqlite.SQLiteQueryBuilder。 android.database.sqlite.SQLiteQueryBuilder.query上的query(SQLiteQueryBuilder.java:515)(SQLiteQueryBuilder.java:392) 在com.example.apkadlapacjenta.HistoriaPomiarow.LoadQuery(HistoriaPomiarow.kt:55)在com.example.apkadlapacjenta.HistoriaPomiarow.onCreate(Historia.com.example.apkadlapacjenta.DbMenager.Query(DbMenager.kt:65) ) 107)在android.os.Looper.loop(Looper.java:214)在android.app.ActivityThread.main(ActivityThread.java:7356)在com.android的java.lang.reflect.Method.invoke(本机方法) .internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492)位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)I / Process:正在发送信号。PID:11261 SIG:9与目标VM断开连接,地址:“ localhost:8630”,传输:“ socket” java:930)I / Process:正在发送信号。PID:11261 SIG:9与目标VM断开连接,地址:“ localhost:8630”,传输:“ socket” java:930)I / Process:正在发送信号。PID:11261 SIG:9与目标VM断开连接,地址:“ localhost:8630”,传输:“ socket”

主要活动


package com.example.apkadlapacjenta

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.SearchManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.SearchView
import androidx.appcompat.app.AppCompatActivity

import kotlinx.android.synthetic.main.activity_historia_pomiarow.*
import kotlinx.android.synthetic.main.content_historia_pomiarow.*
import kotlinx.android.synthetic.main.row.view.deleteBtn
import kotlinx.android.synthetic.main.row1.view.*

@Suppress("DEPRECATION")
class HistoriaPomiarow : AppCompatActivity() {

    var listaWynikow = ArrayList<FIrebaseVal>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_historia_pomiarow)
        setSupportActionBar(toolbar)

        //Wczytujemy baze danych
        LoadQuery("%")
    }

    override fun onResume() {
        super.onResume()
        LoadQuery("%")
    }

    private fun LoadQuery(pomiary: String) {
        val dbManager = DbMenager(this)
        val projections = arrayOf(
            "Id",
            "Stezenie",
            "Zegar",
            "Data",
            "Posilek",
            "Stres",
            "Samopoczucie",
            "Hiperglikemia"
        )
        val selectionArgs = arrayOf(pomiary)
        val cursor =
            dbManager.Query(projections,null,null,null)
        listaWynikow.clear()
        if (cursor.moveToFirst()) {
            do {
                val ID = cursor.getInt(cursor.getColumnIndex("Id"))
                val Stezenie = cursor.getString(cursor.getColumnIndex("Stezenie"))
                val Zegar = cursor.getString(cursor.getColumnIndex("Zegar"))
                val Data = cursor.getString(cursor.getColumnIndex("Data"))
                val Posilek = cursor.getString(cursor.getColumnIndex("Posilek"))
                val Stres = cursor.getString(cursor.getColumnIndex("Stres"))
                val Samopoczucie = cursor.getString(cursor.getColumnIndex("Samopoczucie"))
                val Hiperglikemia = cursor.getString(cursor.getColumnIndex("Hiperglikemia"))
                listaWynikow.add(
                    FIrebaseVal(
                        ID,
                        Stezenie,
                        Zegar,
                        Data,
                        Posilek,
                        Stres,
                        Samopoczucie,
                        Hiperglikemia ))

            } while (cursor.moveToNext())
        }

        //Adapter
        val ListaWynikowAdapter = MylistApdater(this, listaWynikow)
        //ustawienia adaptera
        listapomiarow.adapter = ListaWynikowAdapter

        // Wszystkie zadania
        val total = listapomiarow.count
        //actionbar
        val mActionBar = supportActionBar
        if(mActionBar !=null){
            //ustawienia Action Bara
            mActionBar.subtitle = "Masz $total wyników"
        }
    }
    override fun onCreateOptionsMenu(menu: Menu?):Boolean {
        menuInflater.inflate(R.menu.notemenu, menu)

        //searchView
        val sv: SearchView = menu!!.findItem(R.id.app_bar_search).actionView as SearchView
        val sm = getSystemService(Context.SEARCH_SERVICE) as SearchManager
        sv.setSearchableInfo(sm.getSearchableInfo(componentName))
        sv.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            //implementujemy dwie metody
            override fun onQueryTextSubmit(query: String?): Boolean {
                LoadQuery("%$query%")
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                LoadQuery("%$newText%")
                return false
            }
        })
        return super.onCreateOptionsMenu(menu)
    }
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            R.id.dodaj_notatke->{
                startActivity(Intent(this,AddNote::class.java))
            }
            R.id.sortuj->{
                showSortDialog()
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun showSortDialog() {
        //lista opcji
        val sortOption = arrayListOf("Najnowsze","starsze","Tytul rosnaco","Tytul malejaco")
        val mBuilder = AlertDialog.Builder(this)
        mBuilder.setTitle("Sort by")
        mBuilder.setIcon(R.drawable.ic_sort)

    }




    inner class MylistApdater(
        context: Context,
        private var ListaWynikowAdapter: ArrayList<FIrebaseVal>
    ) : BaseAdapter() {
        var listWynikiArray = ArrayList<FIrebaseVal>()
        var context: Context? = context

        @SuppressLint("ViewHolder","InflateParams")
        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            val myView = layoutInflater.inflate(R.layout.row1, null)
            val Wynik = ListaWynikowAdapter[position]
            myView.stezenienumberpicker.text = Wynik.nodeNumberPicker.toString()
            myView.godzina.text = Wynik.nodezegar
            myView.data.text = Wynik.nodedata
            myView.posilekRG.text = Wynik.nodeposilek
            myView.SP1.text = Wynik.nodehiperglikemia
            myView.SP2.text = Wynik.nodestres
            myView.SP3.text = Wynik.nodesamopoczucie
            //Przycisk kasowania
            myView.deleteBtn.setOnClickListener {
                val dbMenager = DbMenager(this.context!!)
                val selectionArgs = arrayOf(Wynik.nodeID.toString())
                dbMenager.delete("ID=?", selectionArgs)
                LoadQuery("%")
            }
            return myView
        }

        override fun getItem(position: Int): Any {
            return ListaWynikowAdapter[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()

        }

        override fun getCount(): Int {
            return ListaWynikowAdapter.size

        }

    }
    private fun GoToUpdateFun(Wyniki: FIrebaseVal) {
        val intent = Intent(this,AddPomiary::class.java)
        intent.putExtra("Id",Wyniki.nodeID)//wpisz Id
        intent.putExtra("Stezenie",Wyniki.nodeNumberPicker)
        intent.putExtra("Zegar",Wyniki.nodezegar)
        intent.putExtra("Data",Wyniki.nodedata)
        intent.putExtra("Posilek",Wyniki.nodeposilek)
        intent.putExtra("Hiperglikemia",Wyniki.nodehiperglikemia)
        intent.putExtra("Stres",Wyniki.nodestres)
        intent.putExtra("Samopoczucie",Wyniki.nodesamopoczucie)
        startActivity(intent)

    }
}
DBMENAGER

package com.example.apkadlapacjenta

import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.database.sqlite.SQLiteQueryBuilder
import android.widget.RadioGroup
import android.widget.Toast

class DatebaseMenagerPomiary(context: Context) {

    //Database name
    var dbName = "Historia Pomiarów"
    //table name
    var dbTable = "Wyniki"
    //kolumny
    var colID = "Id"
    var colStezenie = "Stezenie"
    var colZegar = "Zegar"
    var colData = "Data"
    var colPosilek = "Posilek"
    var colStres = "Stres"
    var colSamopoczucie = "Samopoczucie"
    var colHiperglikemia = "Hiperglikemia"

    //wersja
    var dbVersion = 1

    //Tworzymy tabele
    val sqlCreateTable =
        "CREATE TABLE$dbTable($colID INTEGER PRIMARY KEY, $colStezenie TEXT, $colZegar TEXT, $colData TEXT, $colPosilek TEXT, $colStres TEXT,$colSamopoczucie TEXT, $colHiperglikemia TEXT)"

    var sqlDB: SQLiteDatabase? = null

    init {
        val db = DatabaseHelperWyniki(context)
        sqlDB = db.writableDatabase
    }


     inner class DatabaseHelperWyniki : SQLiteOpenHelper {
        var context: Context? = null
         constructor(context: Context) : super(context, dbName, null, dbVersion) {
            this.context = context
        }

        override fun onCreate(db: SQLiteDatabase?) {
            db!!.execSQL(sqlCreateTable)
            Toast.makeText(this.context,"baza danych utworzona...",Toast.LENGTH_SHORT).show()

        }

        override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
            db!!.execSQL("zamknij tabele jeśli nie istnieje" + dbTable)
        }


    }

    fun  insert(values:ContentValues):Long{
        val ID  = sqlDB!!.insert(dbTable,"",values)
        return ID
    }
    fun Query(projection:Array<String>,selection:String,selectionArgs:Array<String>,sorOrder:String): Cursor {

        val qb = SQLiteQueryBuilder()
        qb.tables=dbTable
        val cursor = qb.query(sqlDB,projection,selection,selectionArgs,null,null,sorOrder)
        return cursor
    }

    fun delete(selection: String,selectionArgs: Array<String>):Int{
        val count = sqlDB!!.delete(dbTable,selection,selectionArgs)
        return count
    }

    fun update(values:ContentValues,selection: String,selectionArgs: Array<String>): Int {
        val count = sqlDB!!.update(dbTable,values,selection,selectionArgs)
        return count
    }



}
NODE CLASS在名称上看不到:D

package com.example.apkadlapacjenta



class FIrebaseVal(
    nodeID: Int, nodeNumberPicker:String,
    nodezegar:String,
    nodedata:String,
    nodeposilek: String,
    nodehiperglikemia: String,
    nodestres: String,
    nodesamopoczucie: String
    ) {
    var nodeID: Int?= nodeID
    var nodeNumberPicker: String? = nodeNumberPicker
    var nodezegar:String?=nodezegar
    var nodedata:String?=nodedata
    var nodeposilek: String? =nodeposilek
    var nodehiperglikemia:String?=nodehiperglikemia
    var nodestres:String?=nodestres
    var nodesamopoczucie:String?=nodesamopoczucie
}

伴侣您能解释一下我怎么了吗?谢谢

数据库 Android开发
分享到
取消 提交回答
全部回答(1)
  • Puppet
    2020-01-12 11:09:04

    可能您添加了此列,但是忘记了从旧数据库版本添加迁移。您可以通过清除所有应用程序数据(或删除并安装)来检查它。如果重新安装解决了该问题,则应创建从旧数据库版本到新版本的迁移

    另外,您可以重构所有数据库以使用Room而不是难看的ORMLite。它是Google友好而强大的持久性库

    0 0
数据库
使用钉钉扫一扫加入圈子
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题
推荐课程