Не делается запрос в базу SQLite

279
29 декабря 2017, 00:32

Здравствуйте.В проекте было много текста (bb.txt)(где то на 4.5 мб) и я хотел залить их в базу , и уже потом с ними работать.Я поставил файлы в assets при первом запуске данные записываются в базу а дальнейшем достаю их из SQLite.При получении объекта SQLiteDatabase выходит NPE.В чем может быть проблема? Помогите пжлста.

Создание базы :

public class Db  extends SQLiteOpenHelper{
    public Db(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table mytable ("
                + "id integer primary key autoincrement,"
                + "blabla text,"
                + "blabla2 text,"
                + "blabla3 text,"
                + "blabla4 text,"
                + "blabla5 text"+");");
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    }
}   

Запрос к базе :

public class Dao extends Activity implements Runnable{

    Db db = null;
    SQLiteDatabase sqLiteDatabase = null;
    ContentValues cv = new ContentValues();
    @Override
    public void run() {
/*вызываю метод для проверки существует ли база на sdcard*/
        boolean bool = existsDb();
        if(bool){
               /*если база уже есть ничего не делаю.Чтобы не записывать каждый раз при запуске программы*/
        }else{
            db = new Db(Dao.this,"MyDb",null,1);
            sqLiteDatabase = db.getWritableDatabase();
            AssetManager am = this.getAssets();
            try {
                InputStream is = am.open("book.txt");
                InputStream is2 = am.open("book2.txt");
                InputStream is3 = am.open("book3.txt");
                InputStream is4 = am.open("book4.txt");
                InputStream is5 = am.open("book5.txt");
                InputStreamReader reader = new InputStreamReader(is);
                InputStreamReader reader2 = new InputStreamReader(is2);
                InputStreamReader reader3 = new InputStreamReader(is3);
                InputStreamReader reader4 = new InputStreamReader(is4);
                InputStreamReader reader5 = new InputStreamReader(is5);
                BufferedReader bfr = new BufferedReader(reader);
                BufferedReader bfr2 = new BufferedReader(reader2);
                BufferedReader bfr3 = new BufferedReader(reader3);
                BufferedReader bfr4 = new BufferedReader(reader4);
                BufferedReader bfr5 = new BufferedReader(reader5);
                while(bfr.read()!=-1){
                    cv.put("blabla ", bfr.readLine());
                    cv.put("blabla2 ", bfr2.readLine());
                    cv.put("blabla3 ", bfr3.readLine());
                    cv.put("blabla4 ", bfr4.readLine());
                    cv.put("blabla5 ", bfr5.readLine());
                    sqLiteDatabase.insert("mytable", null, cv);
                }
                is.close();
                is2.close();
                is3.close();
                is4.close();
                is5.close();
                reader.close();
                reader2.close();
                reader3.close();
                reader4.close();
                reader5.close();
                bfr.close();
                bfr2.close();
                bfr3.close();
                bfr4.close();
                bfr5.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /*Дальше запрос для проверки.Что данные на текстовых файлах записались*/ 
        Db db2 = new Db(Dao.this,"MyDb",null,1);
        SQLiteDatabase sqLiteDatabase2 = db2.getReadableDatabase();
        String table = "table2";
        String[] columns = {"blabla"};
        String selection = "blabla=?";
        String[] selectionArgs = {"1"};
        String groupBy = null;
        String having = null;
        String orderBy = "column3 DESC";
        String limit = "10";
        Cursor cursor = sqLiteDatabase2.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
        cursor.moveToFirst();
        String ssstr = cursor.getString(0);
        Toast.makeText(Dao.this,"text iz bazy :"+ssstr,Toast.LENGTH_SHORT).show();
    }

/*метод для проверки существует ли база на sdcard*/
public boolean existsDb(){
        File file = new File(String.valueOf(Environment.getDataDirectory())+"data/com.example.zverek.myapplication.services/databases/MyDb.db");
        boolean bool = file.exists();
        return bool;
    }
}

StackTrace : Подумал что это из модели телефона и попробовал почти тот же код на другом аппарате.Тоже ни в какую.

12-27 22:24:00.833 21863-21863/? E/Zygote: Zygote:  error closing descriptor
                                           libcore.io.ErrnoException: close failed: EBADF (Bad file number)
                                               at libcore.io.Posix.close(Native Method)
                                               at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
                                               at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221)
                                               at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879)
                                               at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242)
                                               at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:715)
                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
                                               at dalvik.system.NativeStart.main(Native Method)
12-27 22:24:01.235 21878-21878/? E/cutils-trace: Error opening trace file: No such file or directory (2)
12-27 22:24:01.416 21878-21878/? E/memtrack: Couldn't load memtrack module (No such file or directory)
12-27 22:24:01.416 21878-21878/? E/android.os.Debug: failed to load memtrack module: -2
12-27 22:24:01.526 21891-21891/com.example.zverek.appplicacia E/Zygote: Zygote:  error closing descriptor
                                                                        libcore.io.ErrnoException: close failed: EBADF (Bad file number)
                                                                            at libcore.io.Posix.close(Native Method)
                                                                            at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
                                                                            at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221)
                                                                            at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879)
                                                                            at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242)
                                                                            at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:715)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
                                                                            at dalvik.system.NativeStart.main(Native Method)
12-27 22:24:01.673 21891-21891/com.example.zverek.appplicacia E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
12-27 22:24:01.717 21891-21906/com.example.zverek.appplicacia E/AndroidRuntime: FATAL EXCEPTION: Thread-5890
                                                                                Process: com.example.zverek.appplicacia, PID: 21891
                                                                                java.lang.NullPointerException
                                                                                    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:273)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
                                                                                    at com.example.zverek.appplicacia.Dao.run(Dao.java:43)
                                                                                    at java.lang.Thread.run(Thread.java:841)
12-27 22:24:01.904 21891-21891/com.example.zverek.appplicacia E/ffi_jank: timespan = 42.795616
12-27 22:24:02.416 1386-1386/? E/ffi_jank: timespan = 19.145462
12-27 22:24:05.623 870-1296/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:24:05.763 136-767/? E/FrameworkListener: read() failed (Connection reset by peer)
12-27 22:24:12.608 1386-1386/? E/ffi_jank: timespan = 34.88069
12-27 22:24:13.175 1386-1386/? E/ffi_jank: timespan = 88.55992
12-27 22:24:14.629 21929-21929/com.example.zverek.appplicacia E/Zygote: Zygote:  error closing descriptor
                                                                        libcore.io.ErrnoException: close failed: EBADF (Bad file number)
                                                                            at libcore.io.Posix.close(Native Method)
                                                                            at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
                                                                            at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221)
                                                                            at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879)
                                                                            at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242)
                                                                            at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:715)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
                                                                            at dalvik.system.NativeStart.main(Native Method)
12-27 22:24:14.779 21929-21929/com.example.zverek.appplicacia E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
12-27 22:24:14.820 21929-21944/com.example.zverek.appplicacia E/AndroidRuntime: FATAL EXCEPTION: Thread-5893
                                                                                Process: com.example.zverek.appplicacia, PID: 21929
                                                                                java.lang.NullPointerException
                                                                                    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:273)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
                                                                                    at com.example.zverek.appplicacia.Dao.run(Dao.java:43)
                                                                                    at java.lang.Thread.run(Thread.java:841)
12-27 22:24:15.137 1386-1386/? E/ffi_jank: timespan = 21.26177
12-27 22:24:15.634 870-1375/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:24:15.647 1386-1386/? E/ffi_jank: timespan = 19.490461
12-27 22:24:35.652 870-1126/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:24:45.632 870-1306/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:24:48.136 21975-21975/? E/cutils-trace: Error opening trace file: No such file or directory (2)
12-27 22:24:48.313 21975-21975/? E/memtrack: Couldn't load memtrack module (No such file or directory)
12-27 22:24:48.313 21975-21975/? E/android.os.Debug: failed to load memtrack module: -2
12-27 22:24:48.390 21975-21985/? E/jdwp: jdwp::setsockopt(SO_SNDTIMEO)
12-27 22:24:48.390 21975-21985/? E/jdwp: jdwp::setsockopt(SO_RCVTIMEO)
12-27 22:24:49.569 21991-21991/? E/cutils-trace: Error opening trace file: No such file or directory (2)
12-27 22:24:49.748 21991-21991/? E/memtrack: Couldn't load memtrack module (No such file or directory)
12-27 22:24:49.748 21991-21991/? E/android.os.Debug: failed to load memtrack module: -2
12-27 22:24:49.845 21991-21997/? E/jdwp: Failed sending reply to debugger: Bad file number
12-27 22:24:49.888 22001-22001/? E/Zygote: Zygote:  error closing descriptor
                                           libcore.io.ErrnoException: close failed: EBADF (Bad file number)
                                               at libcore.io.Posix.close(Native Method)
                                               at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
                                               at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221)
                                               at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879)
                                               at com.android.internal.os.ZygoteConnection.runOnce(ZygoteConnection.java:242)
                                               at com.android.internal.os.ZygoteInit.runSelectLoop(ZygoteInit.java:715)
                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
                                               at dalvik.system.NativeStart.main(Native Method)
12-27 22:24:50.080 22001-22001/com.example.zverek.appplicacia E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
12-27 22:24:50.119 22001-22022/com.example.zverek.appplicacia E/AndroidRuntime: FATAL EXCEPTION: Thread-5896
                                                                                Process: com.example.zverek.appplicacia, PID: 22001
                                                                                java.lang.NullPointerException
                                                                                    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:273)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
                                                                                    at com.example.zverek.appplicacia.Dao.run(Dao.java:43)
                                                                                    at java.lang.Thread.run(Thread.java:841)
12-27 22:24:50.434 1386-1386/? E/ffi_jank: timespan = 24.424847
12-27 22:24:50.944 1386-1386/? E/ffi_jank: timespan = 23.218153
12-27 22:25:00.027 1097-1097/? E/MyTag: updateClock : 22:25
12-27 22:25:00.040 1097-1097/? E/MyTag: updateClock : 22:25
12-27 22:25:00.065 1097-1097/? E/MyTag: updateClock : 22:25
12-27 22:25:00.660 15463-15689/? E/CellLocation: create GsmCellLocation
12-27 22:25:00.808 15463-15689/? E/ServerAddressManager: IOException while excuting request:http://lds.lenovomm.com/addr/2.1/query?sid=rpsbrow&didt=imei&did=865698025756086&dm=Lenovo+S850&dv=LENOVO&simcountry=KG&oslocale=ru_RU
12-27 22:25:05.633 870-1294/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:25:15.647 870-1126/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:25:16.467 22055-22055/? E/cutils-trace: Error opening trace file: No such file or directory (2)
12-27 22:25:16.652 22055-22055/? E/memtrack: Couldn't load memtrack module (No such file or directory)
12-27 22:25:16.652 22055-22055/? E/android.os.Debug: failed to load memtrack module: -2
12-27 22:25:25.642 870-881/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:25:35.653 870-1306/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:25:44.907 22082-22082/? E/cutils-trace: Error opening trace file: No such file or directory (2)
12-27 22:25:45.090 22082-22082/? E/memtrack: Couldn't load memtrack module (No such file or directory)
12-27 22:25:45.090 22082-22082/? E/android.os.Debug: failed to load memtrack module: -2
12-27 22:25:45.630 870-1278/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:25:47.953 1358-1358/? E/AccountTypeManager: maods AccountTypeManager.onReceive,intent.phoneName=null,ss=null
12-27 22:25:47.953 1358-1358/? E/AccountTypeManager: maods start to loadAllAccounts
12-27 22:25:47.985 1358-1358/? E/AccountTypeManager: maods AccountTypeManager.onReceive,intent.phoneName=null,ss=null
12-27 22:25:47.985 1358-1358/? E/AccountTypeManager: maods start to loadAllAccounts
12-27 22:25:48.055 1358-1608/? E/ExternalAccountType: Problem reading XML in line 10 for external package com.whatsapp
                                                      com.lenovo.ideafriend.contacts.model.AccountType$DefinitionException: mime type '@2130840568' is already registered
                                                          at com.lenovo.ideafriend.contacts.model.AccountType.addKind(AccountType.java:437)
                                                          at com.lenovo.ideafriend.contacts.model.ExternalAccountType.inflate(ExternalAccountType.java:433)
                                                          at com.lenovo.ideafriend.contacts.model.ExternalAccountType.<init>(ExternalAccountType.java:132)
                                                          at com.lenovo.ideafriend.contacts.model.ExternalAccountType.<init>(ExternalAccountType.java:102)
                                                          at com.lenovo.ideafriend.contacts.model.AccountTypeManagerImpl.loadAccountsInBackground(AccountTypeManager.java:452)
                                                          at com.lenovo.ideafriend.contacts.model.AccountTypeManagerImpl$4.handleMessage(AccountTypeManager.java:320)
                                                          at android.os.Handler.dispatchMessage(Handler.java:110)
                                                          at android.os.Looper.loop(Looper.java:193)
                                                          at android.os.HandlerThread.run(HandlerThread.java:61)
12-27 22:25:48.138 1358-1608/? E/SimCardUtils: ======FeatureOption.LENOVO_DUAL_CARD_SUPPORT=======111============
12-27 22:25:48.139 1358-1608/? E/SimCardUtils: ==simType==1
12-27 22:25:48.140 1358-1608/? E/SimCardUtils: ======FeatureOption.LENOVO_DUAL_CARD_SUPPORT=======111============
12-27 22:25:48.141 1358-1608/? E/SimCardUtils: ==simType==1
12-27 22:25:48.166 1386-1386/? E/ffi_jank: timespan = 26.857
12-27 22:25:48.194 1386-1386/? E/0326: name class ========com.lenovo.launcher.customizer.SearchShortCut
12-27 22:25:48.204 1358-1608/? E/ExternalAccountType: Problem reading XML in line 10 for external package com.whatsapp
                                                      com.lenovo.ideafriend.contacts.model.AccountType$DefinitionException: mime type '@2130840568' is already registered
                                                          at com.lenovo.ideafriend.contacts.model.AccountType.addKind(AccountType.java:437)
                                                          at com.lenovo.ideafriend.contacts.model.ExternalAccountType.inflate(ExternalAccountType.java:433)
12-27 22:25:48.336 22109-22109/? E/Zygote: Zygote:  error closing descriptor
                                           libcore.io.ErrnoException: close failed: EBADF (Bad file number)
                                               at libcore.io.Posix.close(Native Method)
                                               at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
                                               at com.android.internal.os.ZygoteInit.closeServerSocket(ZygoteInit.java:221)
                                               at com.android.internal.os.ZygoteConnection.handleChildProc(ZygoteConnection.java:879)                                                                              java.lang.NullPointerException
                                                                                    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:273)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
                                                                                    at com.example.zverek.appplicacia.Dao.run(Dao.java:43)
                                                                                    at java.lang.Thread.run(Thread.java:841)
12-27 22:25:49.852 1386-1386/? E/ffi_jank: timespan = 24.08454
12-27 22:25:50.363 1386-1386/? E/ffi_jank: timespan = 23.319307
12-27 22:25:53.905 136-767/? E/FrameworkListener: read() failed (Connection reset by peer)
12-27 22:26:00.028 1097-1097/? E/MyTag: updateClock : 22:26
12-27 22:26:00.041 1097-1097/? E/MyTag: updateClock : 22:26
12-27 22:26:00.053 1097-1097/? E/MyTag: updateClock : 22:26
12-27 22:26:05.660 870-1278/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:26:15.655 870-14434/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:26:45.647 870-1126/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:26:48.724 15463-15689/? E/CellLocation: create GsmCellLocation
12-27 22:26:48.926 15463-15689/? E/ServerAddressManager: IOException while excuting request:http://lds.lenovomm.com/addr/2.1/query?sid=rpsbrow&didt=imei&did=865698025756086&dm=Lenovo+S850&dv=LENOVO&simcountry=KG&oslocale=ru_RU
12-27 22:26:58.577 1097-1097/? E/ffi_jank: timespan = 29.558
12-27 22:27:00.021 1097-1097/? E/MyTag: updateClock : 22:27
12-27 22:27:00.026 1097-1097/? E/MyTag: updateClock : 22:27
12-27 22:27:00.044 1097-1097/? E/MyTag: updateClock : 22:27
12-27 22:27:05.650 870-1242/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:27:25.642 870-1348/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:27:55.656 870-1348/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:28:00.047 1097-1097/? E/MyTag: updateClock : 22:28
12-27 22:28:00.065 1097-1097/? E/MyTag: updateClock : 22:28
12-27 22:28:00.081 1097-1097/? E/MyTag: updateClock : 22:28
12-27 22:28:02.262 22268-22268/? E/cutils-trace: Error opening trace file: No such file or directory (2)
12-27 22:28:02.442 22268-22268/? E/memtrack: Couldn't load memtrack module (No such file or directory)
12-27 22:28:02.442 22268-22268/? E/android.os.Debug: failed to load memtrack module: -2
12-27 22:28:15.653 870-1379/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:28:25.638 870-1306/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
12-27 22:28:35.643 870-881/? E/light: setGreenLightScofield---->ffffff--flashMode--0---0----0
READ ALSO
Разбить строку на слова java

Разбить строку на слова java

Здравствуйте, помогите Я прочитал строку из файла и теперь мне надо разбить ее на слова по пробелу, так же в слове поменять буквы местами и записать...

277
Как обновить ACCESS-TOKEN в Retrofit2/RxJava2

Как обновить ACCESS-TOKEN в Retrofit2/RxJava2

Я делаю запрос (любой, авторизация, регистрация и тд) и только потом узнаю, что нужно обновить TOKEN, то есть получаю ошибку 401

279
Перевод из imageView в File

Перевод из imageView в File

Пытаюсь отправить изображение в androidВ java делаю так

294