连接设备时出现Sqlite错误

时间:2018-03-19 14:06:05

标签: android sqlite android-studio

几天前我发布了一个问题,关于sqlite在我的应用程序中说有一个列丢失,尽管我已经正确实现了所有内容。

现在我解决了模拟器上的问题,但如果我使用手机,它会一直对我说以下错误。我已经将wampserver配置编辑为"全部允许"

E/SQLiteLog: (1) table UserDatabase has no column named id
E/SQLiteDatabase: Error inserting userVorname= userAnrede= userPw=test userPraxisAdresszusatz=null userEmail=test userTel= userPraxisPlz=null id=0 userPraxisName=null userPraxisTel=null userPraxisStadt=null userName= userPraxisAdresse=null userTitel=null
              android.database.sqlite.SQLiteException: table UserDatabase has no column named id (code 1): , while compiling: INSERT INTO UserDatabase(userVorname,userAnrede,userPw,userPraxisAdresszusatz,userEmail,userTel,userPraxisPlz,id,userPraxisName,userPraxisTel,userPraxisStadt,userName,userPraxisAdresse,userTitel) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)
                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:895)
                  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:506)
                  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.insertWithOnConflict(SQLiteDatabase.java:1472)
                  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
                  at com.example.kai.appalk.UserDatenbankManager.insertUser(UserDatenbankManager.java:77)
                  at com.example.kai.appalk.Anmelden$3.onResponse(Anmelden.java:169)
                  at com.example.kai.appalk.Anmelden$3.onResponse(Anmelden.java:153)
                  at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:72)
                  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:241)
                  at android.app.ActivityThread.main(ActivityThread.java:6274)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:0)

package com.example.kai.appalk;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Kai on 17.03.2018.
 */

public class UserDatenbankManager extends SQLiteOpenHelper
{
private static final String DB_NAME = "UserDatabase";
private static final int DB_VERSION = 2;
private static final String COL0 = "id";
private static final String COL1 = "userName";
private static final String COL2 = "userVorname";
private static final String COL3 = "userAnrede";
private static final String COL4 = "userTitel";
private static final String COL5 = "userTel";
private static final String COL6 = "userEmail";
private static final String COL7 = "userPraxisName";
private static final String COL8 = "userPraxisAdresse";
private static final String COL9 = "userPraxisPlz";
private static final String COL10 = "userPraxisStadt";
private static final String COL11 = "userPraxisTel";
private static final String COL12 = "userPraxisAdresszusatz";
private static final String COL13 = "userPw";
private static final String SQL_CREATE =
        "CREATE TABLE " + DB_NAME + "(" + COL0 + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COL1 + " TEXT, " + COL2 + " TEXT, " + COL3 + " TEXT, " +
                COL4 + " TEXT, " + COL5 + " TEXT, " + COL6 + " TEXT, " +
                COL7 + " TEXT, " + COL8 + " TEXT, " + COL9 + " TEXT, " +
                COL10 + " TEXT, " + COL11 + " TEXT, " + COL12 + " TEXT, " +
                COL13 + " TEXT);";

public UserDatenbankManager(Context context)
{
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
{
    sqLiteDatabase.execSQL(SQL_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
{

}

public void insertUser(String name, String vorname, String anrede, String titel,
                       String tel, String mail, String pName, String pAdresse,
                       String pPLZ, String pStadt, String pTel, String pAdrZs,
                       String pw)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL0, 0);
    contentValues.put(COL1, name);
    contentValues.put(COL2, vorname);
    contentValues.put(COL3, anrede);
    contentValues.put(COL4, titel);
    contentValues.put(COL5, tel);
    contentValues.put(COL6, mail);
    contentValues.put(COL7, pName);
    contentValues.put(COL8, pAdresse);
    contentValues.put(COL9, pPLZ);
    contentValues.put(COL10, pStadt);
    contentValues.put(COL11, pTel);
    contentValues.put(COL12, pAdrZs);
    contentValues.put(COL13, pw);
    db.insert(DB_NAME, null, contentValues);
}

public void deleteUser()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(DB_NAME, null, null);
}

public void updateUser(String name, String vorname, String anrede, String titel,
                       String tel, String mail, String pName, String pAdresse,
                       String pPLZ, String pStadt, String pTel, String pAdrZs,
                       String pw)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL0, 0);
    contentValues.put(COL1, name);
    contentValues.put(COL2, vorname);
    contentValues.put(COL3, anrede);
    contentValues.put(COL4, titel);
    contentValues.put(COL5, tel);
    contentValues.put(COL6, mail);
    contentValues.put(COL7, pName);
    contentValues.put(COL8, pAdresse);
    contentValues.put(COL9, pPLZ);
    contentValues.put(COL10, pStadt);
    contentValues.put(COL11, pTel);
    contentValues.put(COL12, pAdrZs);
    contentValues.put(COL13, pw);
    db.update(DB_NAME, contentValues, "id=0", null);
}

public Cursor getUser()
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " + DB_NAME, null);
    res.close();
    return res;
}

public String getEmail()
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select " + COL6 + " from " + DB_NAME, null);
    String result = "";
    if (res.moveToFirst())
    {
        do
        {
            result = res.getString(res.getColumnIndex("userEmail"));
        }
        while (res.moveToNext());
    }
    res.close();
    return result;
}

public String getPw()
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select " + COL13 + " from " + DB_NAME, null);
    String result = "";
    if (res.moveToFirst())
    {
        do
        {
            result = res.getString(res.getColumnIndex("userPw"));
        }
        while (res.moveToNext());
    }
    res.close();
    return result;
}

public void updateEmail(String email)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COL6, email);

    db.update(DB_NAME, values, null, null);
}

public void updateTel(String tel)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COL5, tel);

    db.update(DB_NAME, values, null, null);
}

public void updateName(String name)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COL1, name);

    db.update(DB_NAME, values, null, null);
}

public void updatePraxis(String pName, String pAdresse, String pPLZ, String pStadt,
                         String pTel, String pAdrZs)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COL7, pName);
    values.put(COL8, pAdresse);
    values.put(COL9, pPLZ);
    values.put(COL10, pStadt);
    values.put(COL11, pTel);
    values.put(COL12, pAdrZs);

    db.update(DB_NAME, values, null, null);
}

public void updateTitel(String titel)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COL4, titel);

    db.update(DB_NAME, values, null, null);
}

public void updatePasswort(String pw)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COL13, pw);

    db.update(DB_NAME, values, null, null);
}
}


package com.example.kai.appalk;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Anmelden extends AppCompatActivity
{
private String val;
private boolean autologin_b;
private Switch autologin;
private RequestQueue requestQueue;
private String showUserUrl = "http://192.168.212.173/android_connect/showUser.php";
private EditText usernameEditText;
private UserDatenbankManager userDBM;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    DatenbankManager dbm;
    Cursor res;

    dbm = new DatenbankManager(this);
    userDBM = new UserDatenbankManager(this);
    res = dbm.getSwitchValue();
    requestQueue = Volley.newRequestQueue(this.getApplicationContext());
    if (res.getCount() == 0)
    {
        dbm.insertV();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anmelden_gui);
        //getSupportActionBar().setTitle(R.string.login);
    }
    else
    {
        while (res.moveToNext())
        {
            val = res.getString(1);
        }

        if (Integer.parseInt(val) == 1)
        {
            super.onCreate(savedInstanceState);
            startActivity(new Intent(this, Messenger.class));
        }
        else
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_anmelden_gui);
            // getSupportActionBar().setTitle(R.string.login);

            autologin = (Switch) findViewById(R.id.sw_angemeldetBleiben);
            autologin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
            {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b)
                {
                    autologin_b = autologin.isChecked();
                    if (autologin_b)
                    {
                        autologin_b = true;
                    }
                    else
                    {
                        autologin_b = false;
                    }
                }
            });
        }
    }
}

//führt zur Registration, falls Button angeklickt wurde
public void zurRegistration(View view)
{
    startActivity(new Intent(this, Registrieren.class));
}

//Führt zum HomeScreen der App, von dem aus alles gesteuert werden kann
public void zumHomeScreen(View view)
{
    if (checkAnmeldung())
    {
        //hier switch wert in datenbank aktuallisieren
        DatenbankManager db = new DatenbankManager(this);
        if (autologin_b && Integer.parseInt(val) == 0)
        {
            db.updateAutoLogTrue();
        }
        else if (autologin_b && Integer.parseInt(val) == 1)
        {
            db.updateAutoLogFalse();
        }
        checkAnmeldung();
        startActivity(new Intent(this, Messenger.class));
    }
}

//Checkt auf Richtigkeit der eingegebenden Daten
public boolean checkAnmeldung()
{
    boolean result = false;
    usernameEditText = findViewById(R.id.et_username);
    EditText pwEditText = findViewById(R.id.et_passwort);
    getData();
    String sUsername = usernameEditText.getText().toString();
    String sPw = pwEditText.getText().toString();
    if (!sUsername.equals(userDBM.getEmail()) || !sPw.equals(userDBM.getPw()))
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.incorrectData)
                .setCancelable(false)
                .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        //do things
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
    else
    {
        System.out.println(userDBM.getUser());
        result = true;
    }
    return result;
}

public void getData()
{
    System.out.println("getData");
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUserUrl, new Response.Listener<JSONObject>()
    {
        @Override
        public void onResponse(JSONObject response)
        {
            try
            {
                JSONArray users = response.getJSONArray("user");
                for (int i = 0; i < users.length(); i++)
                {
                    JSONObject user = users.getJSONObject(i);
                    String name = user.getString("Email");
                    System.out.println("json: " + name);
                    if (name.equals(usernameEditText.getText().toString()))
                    {
                        if(userDBM.getEmail() == null || userDBM.getEmail().equals(""))
                        {
                            userDBM.insertUser(user.getString("Name"),user.getString("Vorname"),
                                    user.getString("Anrede"), user.getString("Namenszusatz"),
                                    user.getString("Handynr"), user.getString("Email"),
                                    user.getString("Praxis"), user.getString("Adresse"),
                                    user.getString("PLZ"), user.getString("Stadt"),
                                    user.getString("Praxisnr"), user.getString("Adresszusatz"),
                                    user.getString("Passwort"));
                        }
                        else
                        {
                            userDBM.updateUser(user.getString("Name"),user.getString("Vorname"),
                                    user.getString("Anrede"), user.getString("Namenszusatz"),
                                    user.getString("Handynr"), user.getString("Email"),
                                    user.getString("Praxis"), user.getString("Adresse"),
                                    user.getString("PLZ"), user.getString("Stadt"),
                                    user.getString("Praxisnr"), user.getString("Adresszusatz"),
                                    user.getString("Passwort"));
                        }
                    }
                }
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener()
    {
        @Override
        public void onErrorResponse(VolleyError error)
        {

        }
    });
    requestQueue.add(jsonObjectRequest);
}
}

答案 1 :(得分:0)

只需卸载并进行全新安装