Я пытаюсь создать простое приложение для ввода базы данных, в котором, когда я нажимаю кнопку, чтобы вызвать функцию Enter, она должна вставить некоторые значения в базу данных.Но это не работает.
Основная функция нажатия кнопки «Активность» (переменная для базы данных:
SQLiteDatabase db;
private AppDBHelper mhelper;
Глобально:
public void Enter(View V){
mhelper=new AppDBHelper(this);
db=mhelper.getWritableDatabase();
ContentValues val=new ContentValues();
val.put(AppContract.TaskEntry.CONTENT,"thatsall");
val.put(AppContract.TaskEntry.ID,"4");
val.put(AppContract.TaskEntry.STATE,"notStarted");
long err= this.db.insert(AppContract.TaskEntry.TABLE,null,val);
Toast.makeText(getApplicationContext(),"db ="+ err,Toast.LENGTH_SHORT).show();
db.close();
}
AppContract:
public class AppContract {
public static. final String DB_NAME = "storage.db";
public static final int DB_VERSION = 1;
public class TaskEntry implements BaseColumns {
public static final String TABLE = "tasks";
public static final String CONTENT ="content";
public static final String ID ="Id";
public static final String STATE = "state";
}
AppDBhelper:
public class AppDBHelper extends SQLiteOpenHelper
{
public AppDBHelper(Context context) {
super(context, AppContract.DB_NAME, null, AppContract.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + AppContract.TaskEntry.TABLE + " ( " +
AppContract.TaskEntry.ID + " TEXT , " +
AppContract.TaskEntry.STATE + " TEXT NOT NULL," +
AppContract.TaskEntry.CONTENT+ " TEXT )";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + AppContract.TaskEntry.TABLE);
onCreate(db);
}
}
DataProvider:
public class DataProvider extends ContentProvider
{
private AppDBHelper adbhelper;
@Override
public boolean onCreate() {
adbhelper= new AppDBHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
return null;
}
@Override
public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public String getType(Uri uri) {
return null;
}
}
db.insert в mainActivity всегда возвращает -1, что я делаю не так?
Редактировать:вот журнал для кода
10-04 15:32:40.610 9415 9445 I Process Sending signal. PID: 9415 SIG: 9
10-04 15:32:50.538 10144 10144 I art com.mycompany.myapp4 Late-enabling -Xcheck:jni
10-04 15:32:50.631 10144 10144 D TidaProvider com.mycompany.myapp4 TidaProvider()
10-04 15:32:50.647 10144 10144 V BoostFramework com.mycompany.myapp4 BoostFramework() : mPerf = com.qualcomm.qti.Performance@9eeb3f3
10-04 15:32:50.723 10144 10144 W System com.mycompany.myapp4 ClassLoader referenced unknown path: /data/app/com.mycompany.myapp4-1/lib/arm64
10-04 15:32:50.748 10144 10144 W art com.mycompany.myapp4 Class adrt.ADRT failed lock verification and will run slower.
10-04 15:32:50.748 10144 10144 W art com.mycompany.myapp4 Common causes for lock verification issues are non-optimized dex code
10-04 15:32:50.748 10144 10144 W art com.mycompany.myapp4 and incorrect proguard optimizations.
10-04 15:32:50.832 10144 10144 D AccessibilityManager com.mycompany.myapp4 current package=com.mycompany.myapp4, accessibility manager mIsFinalEnabled=false, mOptimizeEnabled=true, mIsUiAutomationEnabled=false, mIsInterestedPackage=false
10-04 15:32:50.839 10144 10144 V BoostFramework com.mycompany.myapp4 BoostFramework() : mPerf = com.qualcomm.qti.Performance@92c7fe3
10-04 15:32:50.839 10144 10144 V BoostFramework com.mycompany.myapp4 BoostFramework() : mPerf = com.qualcomm.qti.Performance@37bcde0
10-04 15:32:50.895 10144 10144 E HAL com.mycompany.myapp4 PATH3 /odm/lib64/hw/gralloc.qcom.so
10-04 15:32:50.895 10144 10144 E HAL com.mycompany.myapp4 PATH2 /vendor/lib64/hw/gralloc.qcom.so
10-04 15:32:50.895 10144 10144 E HAL com.mycompany.myapp4 PATH1 /system/lib64/hw/gralloc.qcom.so
10-04 15:32:50.895 10144 10144 E HAL com.mycompany.myapp4 PATH3 /odm/lib64/hw/gralloc.msm8953.so
10-04 15:32:50.895 10144 10144 E HAL com.mycompany.myapp4 PATH2 /vendor/lib64/hw/gralloc.msm8953.so
10-04 15:32:50.895 10144 10144 E HAL com.mycompany.myapp4 PATH1 /system/lib64/hw/gralloc.msm8953.so
10-04 15:32:50.920 10144 10171 I Adreno com.mycompany.myapp4 QUALCOMM build : 01d2d27, I3d52eaf367
10-04 15:32:50.920 10144 10171 I Adreno com.mycompany.myapp4 Build Date : 12/10/16
10-04 15:32:50.920 10144 10171 I Adreno com.mycompany.myapp4 OpenGL ES Shader Compiler Version: XE031.09.00.03
10-04 15:32:50.920 10144 10171 I Adreno com.mycompany.myapp4 Local Branch :
10-04 15:32:50.920 10144 10171 I Adreno com.mycompany.myapp4 Remote Branch :
10-04 15:32:50.920 10144 10171 I Adreno com.mycompany.myapp4 Remote Branch :
10-04 15:32:50.920 10144 10171 I Adreno com.mycompany.myapp4 Reconstruct Branch :
10-04 15:32:50.925 10144 10171 I OpenGLRenderer com.mycompany.myapp4 Initialized EGL, version 1.4
10-04 15:32:50.925 10144 10171 D OpenGLRenderer com.mycompany.myapp4 Swap behavior 1
10-04 15:32:51.775 10144 10144 V BoostFramework com.mycompany.myapp4 BoostFramework() : mPerf = com.qualcomm.qti.Performance@76280cc
10-04 15:32:51.864 10144 10144 E SQLiteLog com.mycompany.myapp4 (1) table tasks has no column named id
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 Error inserting state=notStarted content=thatsall id=4
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 android.database.sqlite.SQLiteException: table tasks has no column named id (code 1): , while compiling: INSERT INTO tasks(state,content,id) VALUES (?,?,?)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1470)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at com.mycompany.myapp4.MainActivity.Enter(MainActivity.java:53)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at java.lang.reflect.Method.invoke(Native Method)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.view.View$DeclaredOnClickListener.onClick(View.java:4702)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.view.View.performClick(View.java:5619)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.view.View$PerformClick.run(View.java:22295)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.os.Handler.handleCallback(Handler.java:754)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.os.Handler.dispatchMessage(Handler.java:95)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.os.Looper.loop(Looper.java:163)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at android.app.ActivityThread.main(ActivityThread.java:6342)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at java.lang.reflect.Method.invoke(Native Method)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
10-04 15:32:51.865 10144 10144 E SQLiteDatabase com.mycompany.myapp4 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
10-04 15:32:51.878 10144 10144 I Toast com.mycompany.myapp4 Show toast from OpPackageName:com.mycompany.myapp4, PackageName:com.mycompany.myapp4
Также я удалил T_id из БД, и переменная id не предназначена для целей первичного ключа, так как мне не нужен первичный ключ
Редактировать 2: Я обнаружил проблему! Я не обновлял версию БД, а только изменял структуру БД, что вызвало ошибку. Спасибо всем за помощь