Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite Java Android,未找到要删除的列_Java_Android_Database_Sqlite_Android Sqlite - Fatal编程技术网

SQLite Java Android,未找到要删除的列

SQLite Java Android,未找到要删除的列,java,android,database,sqlite,android-sqlite,Java,Android,Database,Sqlite,Android Sqlite,我有一个可以向数据库发送日志的主活动,还有一个可以查看和删除日志的日志活动。当我尝试删除一个日志时,一切正常,程序崩溃,错误是找不到“identity”列来删除它 主要活动: package com.software.roux.diabcalc; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInter

我有一个可以向数据库发送日志的主活动,还有一个可以查看和删除日志的日志活动。当我尝试删除一个日志时,一切正常,程序崩溃,错误是找不到“identity”列来删除它

主要活动:

package com.software.roux.diabcalc;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class MainActivity extends AppCompatActivity {

    SharedPreferences mPrefs;
    SharedPreferences logPrefs;

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
        boolean rb0 = settings.getBoolean("accepted", false);
        if(rb0 == true){

        setTitle("MyInsulin");
        mPrefs=this.getSharedPreferences("settings", 0);
        logPrefs=this.getSharedPreferences("logs", 0);


        final EditText a1 = (EditText) findViewById(R.id.add1);
        final EditText a2 = (EditText) findViewById(R.id.add2);

        final double bolusdose = Double.parseDouble(mPrefs.getString("bolus", "0"));
        final double correctiondose = Double.parseDouble(mPrefs.getString("correction", "0"));
        final double targetlow = Double.parseDouble(mPrefs.getString("low", "0"));
        final double targethigh = Double.parseDouble(mPrefs.getString("high", "0"));
        final double correctto = Double.parseDouble(mPrefs.getString("corrset", "0"));

        String bolusString = mPrefs.getString("bolus", "0");
        String corrString = mPrefs.getString("correction", "0");
        String lowString = mPrefs.getString("low", "0");
        String highString = mPrefs.getString("high", "0");
        String correcttonum = mPrefs.getString("corrset", "0");

        EditText b1 = (EditText)findViewById(R.id.bolus);
        b1.setText(bolusString);
        b1.setEnabled(false);

        EditText b2 = (EditText)findViewById(R.id.correction);
        b2.setText(corrString);
        b2.setEnabled(false);

        EditText b3 = (EditText)findViewById(R.id.targetlow);
        b3.setText(lowString);
        b3.setEnabled(false);

        EditText b4 = (EditText)findViewById(R.id.targethigh);
        b4.setText(highString);
        b4.setEnabled(false);

        EditText b5 = (EditText)findViewById(R.id.correcttonum);
        b5.setText(correcttonum);
        b5.setEnabled(false);

        Button b6 = (Button)findViewById(R.id.setter);
        b6.setEnabled(false);

        Button b8 = (Button)findViewById(R.id.logsave);
        b8.setEnabled(false);

        db = openOrCreateDatabase("logDB", Context.MODE_PRIVATE, null);
        db.execSQL("CREATE TABLE IF NOT EXISTS zlogtable('identity VARCHAR',bslevel VARCHAR,carbs VARCHAR,result VARCHAR);");

        a1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String currentlevelst = a1.getText().toString();
                String carbseatenst = a2.getText().toString();

                if (currentlevelst.length() >= 1) {
                    if (carbseatenst.length() >= 1) {

                        Double currentlevel = Double.parseDouble(a1.getText().toString());
                        Double carbseaten = Double.parseDouble(a2.getText().toString());

                        double firststep = 0;

                        if (currentlevel > targethigh) {
                            firststep = ((currentlevel - correctto) / correctiondose);
                        } else if (currentlevel < targetlow) {
                            firststep = ((currentlevel - targethigh) / correctiondose);
                        } else {
                            firststep = 0;
                        }

                        double secondstep = carbseaten / bolusdose;

                        double firstplussecond = firststep + secondstep;

                        BigDecimal result = new BigDecimal(firstplussecond, MathContext.DECIMAL64);
                        result = result.setScale(2, RoundingMode.CEILING);

                        if (result.compareTo(BigDecimal.ZERO) > 0) {
                            TextView t = (TextView) findViewById(R.id.answerobj);
                            t.setText("Recommended Dose: " + result + " Units");
                            Button b8 = (Button) findViewById(R.id.logsave);
                            b8.setEnabled(true);
                        } else {
                            TextView t = (TextView) findViewById(R.id.answerobj);
                            t.setText("No Insulin Needed");
                        }

                    } else {
                        TextView t = (TextView) findViewById(R.id.answerobj);
                        t.setText("");
                        Button b8 = (Button)findViewById(R.id.logsave);
                        b8.setEnabled(false);
                    }
                } else {
                    TextView t = (TextView) findViewById(R.id.answerobj);
                    t.setText("");
                    Button b8 = (Button)findViewById(R.id.logsave);
                    b8.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        a2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String currentlevelst = a1.getText().toString();
                String carbseatenst = a2.getText().toString();

                if (currentlevelst.length() >= 1) {
                    if (carbseatenst.length() >= 1) {

                        Double currentlevel = Double.parseDouble(a1.getText().toString());
                        Double carbseaten = Double.parseDouble(a2.getText().toString());

                        double firststep = 0;

                        if (currentlevel > targethigh) {
                            firststep = ((currentlevel - correctto) / correctiondose);
                        } else if (currentlevel < targetlow) {
                            firststep = ((currentlevel - targethigh) / correctiondose);
                        } else {
                            firststep = 0;
                        }

                        double secondstep = carbseaten / bolusdose;

                        double firstplussecond = firststep + secondstep;

                        BigDecimal result = new BigDecimal(firstplussecond, MathContext.DECIMAL64);
                        result = result.setScale(2, RoundingMode.CEILING);

                        if (result.compareTo(BigDecimal.ZERO) > 0) {
                            TextView t = (TextView) findViewById(R.id.answerobj);
                            t.setText("Recommended Dose: " + result + " Units");
                            Button b8 = (Button) findViewById(R.id.logsave);
                            b8.setEnabled(true);
                        } else {
                            TextView t = (TextView) findViewById(R.id.answerobj);
                            t.setText("No Insulin Needed");
                        }


                    } else {
                        TextView t = (TextView) findViewById(R.id.answerobj);
                        t.setText("");
                        Button b8 = (Button)findViewById(R.id.logsave);
                        b8.setEnabled(false);
                    }
                } else {
                    TextView t = (TextView) findViewById(R.id.answerobj);
                    t.setText("");
                    Button b8 = (Button)findViewById(R.id.logsave);
                    b8.setEnabled(false);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }else{
        showDialog(0);
    }}

    public void addclickb(View b) throws InterruptedException {

        if (b.getId() == R.id.add2) {

            final EditText a1 = (EditText) findViewById(R.id.add1);
            final EditText a2 = (EditText) findViewById(R.id.add2);

            final double bolusdose = Double.parseDouble(mPrefs.getString("bolus", "0"));
            final double correctiondose = Double.parseDouble(mPrefs.getString("correction", "0"));
            final double targetlow = Double.parseDouble(mPrefs.getString("low", "0"));
            final double targethigh = Double.parseDouble(mPrefs.getString("high", "0"));
            final double correctto = Double.parseDouble(mPrefs.getString("corrset", "0"));

            a2.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                    String currentlevelst = a1.getText().toString();
                    String carbseatenst = a2.getText().toString();

                    if (currentlevelst.length() >= 1) {
                        if (carbseatenst.length() >= 1) {

                            Double currentlevel = Double.parseDouble(a1.getText().toString());
                            Double carbseaten = Double.parseDouble(a2.getText().toString());

                            double firststep = 0;

                            if (currentlevel > targethigh) {
                                firststep = ((currentlevel - correctto) / correctiondose);
                            } else if (currentlevel < targetlow) {
                                firststep = ((currentlevel - targethigh) / correctiondose);
                            } else {
                                firststep = 0;
                            }

                            double secondstep = carbseaten / bolusdose;

                            double firstplussecond = firststep + secondstep;

                            BigDecimal result = new BigDecimal(firstplussecond, MathContext.DECIMAL64);
                            result = result.setScale(2, RoundingMode.CEILING);

                            if (result.compareTo(BigDecimal.ZERO) > 0) {
                                TextView t = (TextView) findViewById(R.id.answerobj);
                                t.setText("Recommended Dose: " + result + " Units");
                                Button b8 = (Button) findViewById(R.id.logsave);
                                b8.setEnabled(true);
                            } else {
                                TextView t = (TextView) findViewById(R.id.answerobj);
                                t.setText("No Insulin Needed");
                            }

                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });






        }
    }

    public void noweditable(View a) {
        if (a.getId() == R.id.editbutton) {
            EditText b1 = (EditText)findViewById(R.id.bolus);
            EditText b2 = (EditText)findViewById(R.id.correction);
            EditText b3 = (EditText)findViewById(R.id.targetlow);
            EditText b4 = (EditText)findViewById(R.id.targethigh);
            EditText b5 = (EditText)findViewById(R.id.correcttonum);
            Button b6 = (Button)findViewById(R.id.setter);
            Button b7 = (Button)findViewById(R.id.editbutton);
            b1.setEnabled(true);
            b2.setEnabled(true);
            b3.setEnabled(true);
            b4.setEnabled(true);
            b5.setEnabled(true);
            b6.setEnabled(true);
            b7.setEnabled(false);
        }
    }

    public void setclick(View b) {
        if (b.getId() == R.id.setter) {

            EditText b1 = (EditText)findViewById(R.id.bolus);
            if (b1.length() > 0) {
                String bolussave = "" + b1.getText().toString();
                SharedPreferences.Editor mEditor1 = mPrefs.edit();
                mEditor1.putString("bolus", bolussave).commit();
            }

            EditText b2 = (EditText)findViewById(R.id.correction);
            if (b2.length() > 0) {
                String corrsave = "" + b2.getText().toString();
                SharedPreferences.Editor mEditor2 = mPrefs.edit();
                mEditor2.putString("correction", corrsave).commit();
            }


            EditText b3 = (EditText)findViewById(R.id.targetlow);
            if (b3.length() > 0) {
                String lowsave = "" + b3.getText().toString();
                SharedPreferences.Editor mEditor3 = mPrefs.edit();
                mEditor3.putString("low", lowsave).commit();
            }


            EditText b4 = (EditText)findViewById(R.id.targethigh);
            if (b4.length() > 0) {
                String highsave = "" + b4.getText().toString();
                SharedPreferences.Editor mEditor4 = mPrefs.edit();
                mEditor4.putString("high", highsave).commit();
            }

            EditText b5 = (EditText)findViewById(R.id.correcttonum);
            if (b4.length() > 0) {
                String corrsetsave = "" + b5.getText().toString();
                SharedPreferences.Editor mEditor5 = mPrefs.edit();
                mEditor5.putString("corrset", corrsetsave).commit();
            }

            b1.setEnabled(false);
            b2.setEnabled(false);
            b3.setEnabled(false);
            b4.setEnabled(false);
            b5.setEnabled(false);

            Button b6 = (Button)findViewById(R.id.setter);
            Button b7 = (Button)findViewById(R.id.editbutton);

            b6.setEnabled(false);
            b7.setEnabled(true);
        }
    }

    public void logview(View b){
        if(b.getId() == R.id.logview)
        {
            Intent myIntent = new Intent(MainActivity.this, Settings.class);
            MainActivity.this.startActivity(myIntent);
        }
    }

    public void logsave(View b){
        if(b.getId() == R.id.logsave)
        {
            EditText l1 = (EditText)findViewById(R.id.add1);
            EditText l2 = (EditText)findViewById(R.id.add2);
            TextView l3 = (TextView)findViewById(R.id.answerobj);


            SharedPreferences.Editor mEditor6 = mPrefs.edit();
            int incrementer = mPrefs.getInt("increment",0);
            incrementer++;
            mEditor6.putInt("increment", incrementer).commit();

            db=openOrCreateDatabase("logDB", Context.MODE_PRIVATE, null);
            db.execSQL("INSERT INTO zlogtable VALUES('"+incrementer+"','"+l2.getText()+"','"+
                    l1.getText()+"','"+l3.getText()+"');");

            Button b8 = (Button)findViewById(R.id.logsave);
            b8.setEnabled(false);
            b8.setText("Saved To Log");
        }
    }
    protected Dialog onCreateDialog(int id){
        // show disclaimer....
        // for example, you can show a dialog box...
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Legal Notice: The content provided through this app is for informational purposes only and does not constitute medical advice. Reliance on any information provided by this application is solely at your own risk.")
                .setCancelable(false)
                .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        // and, if the user accept, you can execute something like this:
                        // We need an Editor object to make preference changes.
                        // All objects are from android.context.Context
                        SharedPreferences settings = getSharedPreferences("PREFS_NAME", 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putBoolean("accepted", true);
                        // Commit the edits!
                        editor.commit();
                    }
                })
                .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //nm.cancel(R.notification.running); // cancel the NotificationManager (icon)
                        System.exit(0);
                    }
                });
        AlertDialog alert = builder.create();
        return alert;
    }
}
package com.software.roux.diabcalc;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class Settings extends AppCompatActivity {

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        setTitle("My Logs");


        db=openOrCreateDatabase("logDB", Context.MODE_PRIVATE, null);
        Cursor c=db.rawQuery("SELECT * FROM zlogtable", null);
        if(c.getCount()==0)
        {
            System.out.print("Error");
            return;
        }
        StringBuffer buffer=new StringBuffer();
        while(c.moveToNext())
        {
            final TextView logfield = new TextView(this);
            final LinearLayout layout = (LinearLayout) findViewById(R.id.loghold);
            final LinearLayout layout1 = new LinearLayout(this);
            final int identifier = c.getInt(0);
            logfield.append("Blood Sugar Level: "+c.getString(2)+"\n");
            logfield.append("Carbs Eaten: "+c.getString(1)+"\n");
            logfield.append(""+c.getString(3)+"\n");
            final Button deleter = new Button(this);
            deleter.setText("Delete");
            deleter.setTextSize(12);

            layout1.addView(logfield);
            layout1.addView(deleter);
            layout.addView(layout1);

            deleter.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    layout1.removeView(deleter);
                    layout1.removeView(logfield);
                    db=openOrCreateDatabase("logDB", Context.MODE_PRIVATE, null);
                    db.execSQL("DELETE FROM zlogtable WHERE identity = "+identifier+" ");
                }
            });
        }
        TextView log1 = (TextView) findViewById(R.id.log1);
        log1.setText(buffer.toString());

    }

    public void switchclick(View a) {
        if (a.getId() == R.id.backbutton) {
            Intent myIntent = new Intent(Settings.this, MainActivity.class);
            Settings.this.startActivity(myIntent);
        }
    }





}

数据库中似乎没有名为“identity”的列。您可以检查db表的列名吗。

Rotwang评论并发现此语句存在错误,我删除了字符串标记,它工作正常:


这是错误的:“如果不存在创建表zlogtable('identity VARCHAR')。请删除“字符串标记”。

您以前是否创建了没有列的表
identity
。如果是,则代码不会使用新列再次创建表。这是错误的:
“如果不存在创建表zlogtable”('identity VARCHAR'
。删除'string标记。