简体   繁体   中英

Why it does not create the table and add data?

DatabaseHelper.java:

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

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final String TAG = "DatabaseHelper";
    public static final String DATABASE_NAME = "daysDATABASE.db";
    public static final String TABLE_NAME = "daysDATABASE";
    private static final String COL1 = "datez";
    private static final String COL2 = "json";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, datez TEXT, json TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean addData(String date, String json) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("datez", date);
        contentValues.put("json", json);
        db.insert(TABLE_NAME, null, contentValues);
        return true;
    }

MainActivity.java:

public class MainActivity extends AppCompatActivity {
DatabaseHelper mydb;

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

        mydb = new DatabaseHelper(this);
        mydb.addData("02164987", "adsdsadsadsadsadsadsa");
    }

If I open the database, there is no table in it, nor data ofc.. since the table is not created.

..........................................................................................................................................................

I would tell more informations but I do not think you need more than this 2 java class. I copy pasted and changed some stuff from tutorialspoint tutorial but seems like it does not work for some reason.

Even if your table was created, you could not add data in it with the addData() method because you don't supply a value for the id column.
The CREATE statement creates the column id as PRIMARY KEY but not AUTOINCREMENT so you must supply its value. But I don't think that this is what you want.
So change the CREATE statement to this:

db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY AUTOINCREMENT, datez TEXT, json TEXT)");

then uninstall the app from the emulator/device so the db is deleted and run again.
This way the onCreate() method will be executed and the table will be created.
Edit1 : remember to close the database object after you're done with it:

mydb.close()

Edit2 : the insert() method, if successful, returns the id of the new added row or -1 if there was a problem, so I suggest you make this change in addData() method:

int id = db.insert(TABLE_NAME, null, contentValues); 
return (id >= 0);

Adding to forpas answer which solves the root cause of the problem, i will suggest you use

db.insertOrThrow(TABLE_NAME, null, contentValues);

instead of db.insert(TABLE_NAME, null, contentValues); so you can get a clue of what the problem might be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM