R
R
Roman Kavyrshin2016-02-29 19:28:51
Android
Roman Kavyrshin, 2016-02-29 19:28:51

Why is my SQLite connection not working?

For some reason it refuses to work with the database.

public class MainActivity extends AppCompatActivity {

    DB db;
    SimpleCursorAdapter sca;
    Cursor cursor;
    ListView listView;


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new DB(this);
        db.open();

        cursor = db.getAllData();
        startManagingCursor(cursor);

        String[] from = new String[] { DB.COLUMN_PREV_IMG, DB.COLUMN_HEADER, DB.COLUMN_SHORT_TEXT, DB.COLUMN_DATE };
        int[] to = new int[] { R.id.prev_img, R.id.tvHeader, R.id.tvShortText, R.id.tvDate };

        sca = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(sca);
    }

    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }
    }

DB.java class code
public class DB {

    private static final String DB_NAME = "mydb";
    private static final int DB_VERSION = 5;
    private static final String DB_TABLE = "mytable";

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_IMG = "img_id";
    public static final String COLUMN_PREV_IMG = "prev_image_id";
    public static final String COLUMN_LINK = "link";
    public static final String COLUMN_HEADER = "header";
    public static final String COLUMN_FULL_TEXT = "full_text";
    public static final String COLUMN_SHORT_TEXT = "short_text";
    public static final String COLUMN_DATE = "date";

    private static final String DB_CREATE =
            "create table " + DB_TABLE + "("
                    + COLUMN_ID + " integer primary key," + COLUMN_IMG + " integer,"
                    + COLUMN_PREV_IMG + " integer," + COLUMN_LINK + " text"
                    + COLUMN_HEADER + " text," + COLUMN_FULL_TEXT + " text"
                    + COLUMN_SHORT_TEXT + " text," + COLUMN_DATE + " text" + ");";

    private final Context mCtx;
    private DBHelper DBHelper;
    private SQLiteDatabase DB;

    public DB(Context ctx) {
        mCtx = ctx;
    }

    public void open() {
        DBHelper = new DBHelper(mCtx, DB_NAME, null, DB_VERSION);
        DB = DBHelper.getWritableDatabase();
    }

    public void close() {
        if (DBHelper!=null) DBHelper.close();
    }

    public Cursor getAllData() {
        return DB.query(DB_TABLE, null, null, null, null, null, null);
    }

    public void addRec(int id, int img, int prevImg, String link, String header, String fullText,
                       String shortText, String date) {
        ContentValues cv = new ContentValues();
        cv.put(COLUMN_ID, id);
        cv.put(COLUMN_IMG, img);
        cv.put(COLUMN_PREV_IMG, prevImg);
        cv.put(COLUMN_LINK, link);
        cv.put(COLUMN_HEADER, header);
        cv.put(COLUMN_FULL_TEXT, fullText);
        cv.put(COLUMN_SHORT_TEXT, shortText);
        cv.put(COLUMN_DATE, date);
        DB.insert(DB_TABLE, null, cv);
    }

    public void delRec(long id) {
        DB.delete(DB_TABLE, COLUMN_ID + " = " + id, null);
    }

    private class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
                        int version) {
            super(context, name, factory, version);
        }

        // создаем и заполняем БД
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DB_CREATE);

            ContentValues cv = new ContentValues();
            // заполним таблицу
            cv.put("_id", 10);
            cv.put("img_id", R.drawable.first);
            cv.put("prev_img_id", R.drawable.first);
            cv.put("link", "blablaLink");
            cv.put("header", "title1");
            cv.put("full_text", "fulltext1");
            cv.put("short_text", "shorttext1");
            cv.put("date", "1456754040029");
            DB.insert("mytable", null, cv);

            cv.put("_id", 11);
            cv.put("img_id", R.drawable.second);
            cv.put("prev_img_id", R.drawable.second);
            cv.put("link", "blablaLink2");
            cv.put("header", "title2");
            cv.put("full_text", "fulltext2");
            cv.put("short_text", "shorttext2");
            cv.put("date", "1456754030029");
            DB.insert("mytable", null, cv);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if(oldVersion != newVersion) {
                Log.d("myLogs", "Обновляемся с версии " + oldVersion + " на версию " + newVersion);

                // Удаляем старую таблицу и создаём новую
                db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
                // Создаём новую таблицу
                onCreate(db);
            }
        }
    }
    }

Writes to the log that there is a problem with db.open();
getWritableDatabase();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question