Ich mache eine App, die der Benutzer einige Informationen in Nur-Text-Felder eingeben und sie zu einer Datenbank hinzufügen wird. Der Benutzer kann auch auf die Schaltfläche Show DB klicken und der Inhalt der Tabelle wird auf dem Bildschirm angezeigt. Aber aus irgendeinem Grund kann ich keine Informationen in meiner Datenbank hinzufügen. Ich habe sogar mit dem SQL-Manager von firefoxe gecheckt. Zuerst ist die Hauptaktivität und Sekunden ist die DBHelper-Klasse. Danke im Voraus. (Wenn jemand will, kann ich eine ZIP-Datei mit der App nur 300 kb gereinigt senden)Daten werden nicht in SQLite-Datenbank hinzugefügt
package com.example.pavleas.testsqlite;
import android.database.Cursor;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//instance creation
DBHelper movDB;
//define 4 variables for texts and two for button
EditText editName, editYear, editGenre, editRated;
Button btnAdd, btnView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//new instance
//calls constructor of DBHelper class, and in the constructor we are creating the database
//and table
movDB = new DBHelper(this); //argument is the context
editName = (EditText)findViewById(R.id.editText_Name);
editYear = (EditText)findViewById(R.id.editText_Year);
editGenre = (EditText)findViewById(R.id.editText_Genre);
editRated = (EditText)findViewById(R.id.editText_Rated);
btnAdd = (Button)findViewById(R.id.button_Add);
btnView = (Button)findViewById(R.id.button_View);
AddValues();
viewAll();
}
//after casting is done, btnAdd will add it, this is what this method does
public void AddValues(){
btnAdd.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
//calls adddata method using instance DBHelper class and takes values from editTexts
boolean isInserted = movDB.addData(editName.getText().toString(),
editYear.getText().toString(),
editGenre.getText().toString(),
editRated.getText().toString());
if (isInserted = true)
Toast.makeText(MainActivity.this,"Data inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not inserted", Toast.LENGTH_LONG).show();
}
}
);
}
//method for calling set on click listener
public void viewAll(){
btnView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = movDB.getData();
if(res.getCount()==0) {
//method for showinf error
showMessage("Error","No data found");
return;
}
StringBuffer buffer = new StringBuffer();
//moves to the nexrt result
while (res.moveToNext()){
//take next result and store it to buffer
buffer.append("ID :"+res.getString(0)+"\n");//0 is index of column
buffer.append("MovieName :"+res.getString(1)+"\n");
buffer.append("YearReleased :"+res.getString(2)+"\n");
buffer.append("Genre :"+res.getString(3)+"\n");
buffer.append("Rated :"+res.getString(4)+"\n\n"); //double line to notice next data
}
//show all data
showMessage("Data", buffer.toString());
}
}
);
}
public void showMessage(String title, String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
========================= =======================================
package com.example.pavleas.testsqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Pavleas on 27/5/2017.
*/
//extend using sqlite helper
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MoviesInfo.db"; //database name
public static final String TABLE_NAME = "MoviesData"; //table name
//columns
public static final String COL1 = "ID";
public static final String COL2 = "MovieName";
public static final String COL3 = "YearReleased";
public static final String COL4 = "Genre";
public static final String COL5 = "Rated";
//constructor
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//query for table creation
db.execSQL("create table "+ TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, MovieName TEXT," +
" YearReleased INTEGER, GENRE TEXT, RATED TEXT); ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//drops table if it already exists
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
//method to insert data
// call function which creats the database.
public boolean addData(String name, String year, String genre, String rated){
SQLiteDatabase db = this.getWritableDatabase();
//instance of our class called content value
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, name);
contentValues.put(COL3, year);
contentValues.put(COL4, genre);
contentValues.put(COL5, rated);
//using db instance we insert data
//if data is not be insurted, ,method will return -1, so i defined it as long
long result = db.insert(TABLE_NAME,null ,contentValues);
if (result == -1)
return false;
else
return true;
}
//for displaying data
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
//instance of cursor class
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
}
EDIT 1:
05-28 04:27:21.239 4006-4006/? I/art: Not late-enabling -Xcheck:jni (already on)
05-28 04:27:21.317 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.497 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.537 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.555 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.593 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.615 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.646 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.667 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.685 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.712 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.732 4006-4006/com.example.pavleas.testsqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=x86 --instruction-set-features=smp,ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.example.pavleas.testsqlite-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/x86/[email protected]@[email protected][email protected]) because non-0 exit status
05-28 04:27:21.734 4006-4006/com.example.pavleas.testsqlite W/System: ClassLoader referenced unknown path: /data/app/com.example.pavleas.testsqlite-2/lib/x86
05-28 04:27:21.736 4006-4006/com.example.pavleas.testsqlite I/InstantRun: starting instant run server: is main process
05-28 04:27:21.858 4006-4006/com.example.pavleas.testsqlite W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-28 04:27:22.210 4006-4062/com.example.pavleas.testsqlite D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 05-28 04:27:22.213 4006: 4006 D/ ]
HostConnection::get() New Host Connection established 0xaa995180, tid 4006
[ 05-28 04:27:22.261 4006: 4062 D/ ]
HostConnection::get() New Host Connection established 0xac3d2c00, tid 4062
05-28 04:27:22.267 4006-4062/com.example.pavleas.testsqlite I/OpenGLRenderer: Initialized EGL, version 1.4
05-28 04:27:22.267 4006-4062/com.example.pavleas.testsqlite W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
05-28 04:27:22.421 4006-4006/com.example.pavleas.testsqlite W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-28 04:27:42.727 4006-4006/com.example.pavleas.testsqlite E/SQLiteLog: (1) table MoviesData has no column named MovieName
05-28 04:27:42.729 4006-4006/com.example.pavleas.testsqlite E/SQLiteDatabase: Error inserting Rated=r MovieName=logan Genre=action YearReleased=2017
android.database.sqlite.SQLiteException: table MoviesData has no column named MovieName (code 1): , while compiling: INSERT INTO MoviesData(Rated,MovieName,Genre,YearReleased) VALUES (?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
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:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.pavleas.testsqlite.DBHelper.addData(DBHelper.java:58)
at com.example.pavleas.testsqlite.MainActivity$1.onClick(MainActivity.java:46)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-28 04:27:42.853 4006-4062/com.example.pavleas.testsqlite V/RenderScript: 0xa26bb000 Launching thread(s), CPUs 2
05-28 04:27:46.258 4006-4062/com.example.pavleas.testsqlite E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa9affe0
'... aus irgendeinem Grund ...' ** WILD GUESS ** (da Sie Ihr Logcat nicht hochgeladen haben): Sie haben Ihre Tabellenstruktur ** nach ** einem ersten Lauf geändert. –
Obwohl Sie hier sind hart und verletzt noob Programmierer Gefühle, Sie sind wirklich hilfreich. Ich deinstallierte meine apk dann lief es wieder und es funktionierte :) danke mann – paulaxa1
... harsch ?! Was meinen Sie? –