마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

android sqlite에 image 경로를 넣어줘서 경로를 불러오고 싶은데 에러가 납니다. 고수님들 제발 알려주세요 ㅠㅠ

0 추천
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkSelfPermission();

        myHelper = new myDBHelper(this);
        btnImage1 = (ImageView) findViewById(R.id.btnImage);
        text1 = (TextView) findViewById(R.id.text);
        button1 = (Button) findViewById(R.id.button);



//
//        BitmapFactory.Options options = new BitmapFactory.Options();
//        Bitmap originalBm = BitmapFactory.decodeFile(tempFile.getAbsolutePath(), options);
//        btnImage1.setImageBitmap(originalBm);


        btnImage1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType(MediaStore.Images.Media.CONTENT_TYPE);
                startActivityForResult(intent, PICK_FROM_ALBUM);

            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String strNames = null;
                sqlDB = myHelper.getReadableDatabase();
                Cursor cursor;
                cursor = sqlDB.rawQuery("SELECT*FROM address123", null);
                //sqlFile = new File(cursor1.getString(1));
                while(cursor.moveToNext()) {
                    strNames += cursor.getString(1);
                }
                text1.setText(strNames);
                Log.d(TAG,text1.getText().toString());

                cursor.close();
                sqlDB.close();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PICK_FROM_ALBUM) {
            Uri photoUri = data.getData();
            Cursor cursor = null;

            try {
                String[] proj = {MediaStore.Images.Media.DATA};

                assert photoUri != null;
                cursor = getContentResolver().query(photoUri, proj, null, null, null);

                assert cursor != null;
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                cursor.moveToFirst();

                tempFile = new File(cursor.getString(column_index));

                str2 = cursor.getString(column_index);
                insert(str2);
                Toast.makeText(getApplicationContext(), "입력됨", 0).show();

                text1.setText(cursor.getString(column_index));  // 경로
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }

            setImage();
        }
    }

    // sqlite 입력
    public void insert(String address) {
        sqlDB = myHelper.getWritableDatabase();

        ContentValues value = new ContentValues();
        value.put("name",address);
        sqlDB.insert("address123",null, value);
    }

    // 갤러리에서 받아온 이미지 넣기
    private void setImage() {

        btnImage1 = findViewById(R.id.btnImage);

        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap originalBm = BitmapFactory.decodeFile(tempFile.getAbsolutePath(), options);

        btnImage1.setImageBitmap(originalBm);
    }

    // sqlite 이용
    public class myDBHelper extends SQLiteOpenHelper {
        public myDBHelper(Context context) {
            super(context, "addressDB",null,1);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql = "create table address123 (_id integer primary key autoincrement, name text)";
            db.execSQL(sql);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            String sql = "drop table if exists address123";
            db.execSQL(sql);
            onCreate(db);
        }
    }

 

sqlite에 imagepath를 넣어 저장하려고 하는데 버튼을 눌러 경로를 불러올때 에러가 나네요...

계속 고민해봤는데도 도저히 모르겠어서 고수님들에게 물어봅니다. 아니면 저의 방식이 잘못된 것인가요?

 아직 여기에 구현하지는 않았지만 제가 원하는 것은 id당 image경로를 따로따로 불러오고 싶은데 이거에 대한 방법도 알려주시면 감사하겠습니다.(꾸벅)

정말로 이 에러에 대해 혹은 이 방법에 대해 문제점에 대해 알려주시면 감사하겠습니다. 일주일동안 이 상태에 멈춰 있어요 ㅠㅠ

 

jae7151 (120 포인트) 님이 2020년 10월 30일 질문
이것은 로그캣 에러메시지입니다.


2020-10-30 16:40:35.886 470-470/? E/netmgr: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:network' service: Invalid argument
2020-10-30 16:40:35.886 470-470/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2020-10-30 16:40:37.514 402-402/? E/Layer: [Surface(name=Task=310)/@0x593d325 - animation-leash#0] No local sync point found
2020-10-30 16:40:37.514 402-402/? E/Layer: [Surface(name=Task=1)/@0xadbc6c6 - animation-leash#0] No local sync point found
2020-10-30 16:40:38.174 500-500/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2020-10-30 16:40:38.174 500-500/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
2020-10-30 16:40:41.152 265-269/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-10-30 16:40:41.156 265-269/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-10-30 16:40:43.282 14047-14047/? E/oid.setupwizar: Not starting debugger since process cannot load the jdwp agent.
2020-10-30 16:40:44.638 14009-14009/com.example.imagealbum E/SQLiteLog: (1) no such table: address123 in "INSERT INTO address123(name) VALUES (?)"
2020-10-30 16:40:44.641 14009-14009/com.example.imagealbum E/SQLiteDatabase: Error inserting name=/storage/emulated/0/DCIM/Camera/IMG_20201023_194453.jpg
    android.database.sqlite.SQLiteException: no such table: address123 (code 1 SQLITE_ERROR): , while compiling: INSERT INTO address123(name) VALUES (?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1699)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1570)
        at com.example.imagealbum.MainActivity.insert(MainActivity.java:145)
        at com.example.imagealbum.MainActivity.onActivityResult(MainActivity.java:125)
        at android.app.Activity.dispatchActivityResult(Activity.java:8305)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2020-10-30 16:40:48.371 14009-14009/com.example.imagealbum E/SQLiteLog: (1) no such table: address123 in "SELECT*FROM address123"
2020-10-30 16:40:48.372 14009-14009/com.example.imagealbum E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.imagealbum, PID: 14009
    android.database.sqlite.SQLiteException: no such table: address123 (code 1 SQLITE_ERROR): , while compiling: SELECT*FROM address123
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1545)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1484)
        at com.example.imagealbum.MainActivity$2.onClick(MainActivity.java:91)
        at android.view.View.performClick(View.java:7448)
        at android.view.View.performClickInternal(View.java:7425)
        at android.view.View.access$3600(View.java:810)
        at android.view.View$PerformClick.run(View.java:28296)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2020-10-30 16:40:51.312 563-619/? E/EGL_emulation: eglQueryContext 32c0  EGL_BAD_ATTRIBUTE
2020-10-30 16:40:51.312 563-619/? E/EGL_emulation: tid 619: eglQueryContext(1909): error 0x3004 (EGL_BAD_ATTRIBUTE)
2020-10-30 16:40:51.824 265-269/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-10-30 16:40:51.827 265-269/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-10-30 16:40:51.834 563-612/? E/KernelCpuSpeedReader: Failed to read cpu-freq: /sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state: open failed: ENOENT (No such file or directory)
2020-10-30 16:40:51.840 265-269/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup34: Permission denied
2020-10-30 16:40:51.843 265-269/? E/android.system.suspend@1.0-service: Error opening kernel wakelock stats for: wakeup35: Permission denied
2020-10-30 16:40:51.853 563-619/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0x0
2020-10-30 16:40:51.994 770-1201/? E/bt_btm: BTM_BleObserve Observe not active

1개의 답변

0 추천
no such table: address123 in "INSERT INTO address123(name) VALUES (?)"

address123이라는 테이블이 db에 없어서 나는 에러네요. 해당 테이블이 생성이 안되었거나 이름이 틀리거나 했겠네요. 가능하시면 ROOM를 쓰세요, SQL문 땜에 시간 낭비하지 않으셔도 됩니다. 아니면 직접 쿼리문을 작성하지 마시고 간단한 QueryBuilder 클래스를 만드셔서 필요한 정보만 넘기면 쿼리문이 생성되도록 해보세요. 이렇게 하면 여러군데서 재사용이 가능하고 QueryBuilder 만 따로 테스트 할 수도 있으실 테구요.

 

spark (226,720 포인트) 님이 2020년 10월 30일 답변
...