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

자꾸 no such table이 떠요 ㅠㅠㅠㅠㅠㅠㅠ 제발도와주세요ㅠㅠㅠㅠㅠㅠ [closed]

0 추천
package net.bibim.listview;

import java.net.URISyntaxException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ListViewTestActivity extends Activity {
 EditText editText1;
 Button btnSave;

 Button stopser;
 
 SimpleCursorAdapter adapter1=null;

 static ArrayList<String> arrayList;
 static ArrayAdapter<String> adapter;
 ListView listView;
 ClipboardManager cm;
 Intent intent;
 
 /* basic_info 테이블 생성 쿼리 */
 private static final String Q_CREATE_TABLE= "CREATE TABLE test (" +
         "title TEXT PRIMARY KEY," +
   "contents TEXT," +
         ");";
 
 private final String Q_GET_LIST= "SELECT * FROM test" + " ORDER BY _id DESC"; 
// DBManagerHandler handler = new DBManagerHandler(getApplicationContext());
 
// Cursor cs;

 BroadcastReceiver receiver;
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
//  cs = handler.select();

  stopser = (Button) findViewById(R.id.stopservice);

  btnSave = (Button) findViewById(R.id.btnSave);

  arrayList = new ArrayList<String>();

  adapter = new ArrayAdapter<String>(ListViewTestActivity.this,
    android.R.layout.simple_list_item_1, arrayList);

  listView = (ListView) findViewById(R.id.listView);
  listView.setAdapter(adapter);

  // 기본적인 서비스 만들어보기
  Log.i("TAG", "Controller Activity - startService()");
  Intent myserviceintent = new Intent(this, ServiceApp.class);

  startService(myserviceintent);

  // 종료버튼
  stopser.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(ListViewTestActivity.this,
      ServiceApp.class);

    stopService(intent);
   }
  });

  /* 저장 버튼 */
  btnSave.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    intent = new Intent(ListViewTestActivity.this, EditClass.class);

    startActivityForResult(intent, 0); // 에디트클래스의 엑티비티에서 다시
             // 돌아옴으로 포리조트함수사용

   }
  });

  

 }// 수정 한뒤
 
 
 protected void onResume(){
     super.onResume();
     getDbData();
    }
 
  private void getDbData(){
      SQLiteDatabase db=null;
      if(db==null){
       db=openOrCreateDatabase("test.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
      }
      
      checkTableIsCreated(db); //db에 solo_info 테이블이 있는지 확인
      
      Cursor c=db.rawQuery(Q_GET_LIST, null);
      //startManagingCursor(c);
      
      listView=(ListView) findViewById(R.id.listView);
     /* adapter1=new SimpleCursorAdapter(
        getApplicationContext(),
        android.R.layout.simple_list_item_2,
        c,
        new String[] {"title", "contents"},
        new int[] {android.R.id.text1, android.R.id.text2}   
        );*/
      listView.setAdapter(adapter1);
      //listView.setDivider(new ColorDrawable(Color.rgb(149, 145, 145)));
      //listView.setDividerHeight(2);
      
      
      if(db!=null){
       db.close();  
      }
     }
  
  /* DB에 테이블 존재 유무 확인 함수 */
     private void checkTableIsCreated(SQLiteDatabase db){
      Cursor c=db.query("test", new String[]{"count(*)"}, "name=?",
        new String[]{"test"}, null, null, null,null);  //시스템 카탈로그에서 커서로 테이블 이름을 확인
      //Cursor c=db.query("test",null,null,null, null, null, null);  //시스템 카탈로그에서 커서로 테이블 이름을 확인
      Integer cnt=0;
      c.moveToFirst();
      while(c.isAfterLast()==false){
       cnt=c.getInt(0);
       c.moveToNext();
      }
      c.close();
      
      if(cnt==0){
       db.execSQL(Q_CREATE_TABLE); //DB에 테이블이 없으면 테이블 생성 쿼리 실행
      }
     }

로그캣 복사하는 법을 몰라서 손으로 쓸께요 ㅠㅠ

error opening trace file:No such file or directory (2)

(1) no such table: test

FATAL EXCEPTION:main

java.lang.RuntimeException:Unable to resume activity {net.bibim.listview/net.bibim.listview.ListViewTestActivity}: android.dababase.sqlite,SQLiteException:no such tabe:test (code 1): , while compiling: SELE CT count(*) FROM test WHERE name=?

여기서 while~ 부분이 caused by에서도 뜨구요 ㅜㅜㅜ

이상한게 소스 코드의 Onresume을 주석 처리하고 한번 돌린 후에 (이경우에는 저장이 안됩니다 ㅜㅜ)

다시 살리고 돌리면 실행이 돼요 ㅠㅠ

주석처리 안하고 저 코드가 오류없이 돌아갈 수 있는 방법이 없을까요 ㅠㅠ

 

질문을 종료한 이유: 답변이 달렸습니당
초보 안드롱 (480 포인트) 님이 2014년 5월 24일 질문
초보 안드롱님이 2014년 6월 1일 closed

1개의 답변

+1 추천
 
채택된 답변
말그대로 table이 없다는말이네요.

테이블 생성이 안되었으니 create table쪽에 문제가 있겠네요.

 

private static final String Q_CREATE_TABLE= "CREATE TABLE test (" +         "title TEXT PRIMARY KEY," +   "contents TEXT," +         ");";

 

쿼리문에 일단 문제가 contents TEXT , )이렇게 컴마를 찍고 닫아버리니 생성이 되지 않을거같은데요.

그리고 CREATE TABLE보단 CREATE TABLE IF NOT EXISTS 를 써서, 테이블이 없을때만 생성되도록 하는게 낫습니다.

그럼 아래 테이블 생성유무를 체크할 필요가 없어지는데요. Crate table if not exists로 생성했을때 리턴값이 실패로뜨면 오류처리해주면되니깐요..
기초개발자 (24,060 포인트) 님이 2014년 5월 25일 답변
초보 안드롱님이 2014년 5월 31일 채택됨
감사합니다 ㅜㅜㅜ 지금 해보려고 하는데 잘되면 진짜 복받으실꺼에요 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ 잘안돼두 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ 감사합니다 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ
혹시 title이 예약어인지 모르겠는데, title대신 다른단어를 넣어보세요.
title2 text, contets2 text로 해보세요.

create table if not exists test (_id integer primary key autoincrement, title2 text, contents2 text);
private static final String Q_CREATE_TABLE= "CREATE TABLE IF NOT EXISTS "  +  TB_NAME+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT "
            + "," + "title TEXT "
            + "," + "contents TEXT"
            + ");";
    ///////////////////
private void getDbData(){
            SQLiteDatabase db=null;
            if(db==null){
                db=openOrCreateDatabase("test.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
            }
           
            //checkTableIsCreated(db); //db에 solo_info 테이블이 있는지 확인
            db.execSQL(Q_CREATE_TABLE);
            Cursor c=db.rawQuery(Q_GET_LIST, null);
댓글은 못봤었는데 맨처음 답글 달아주신거 참고로 해서 위 소스처럼 고쳤더니 실행됬어요 ㅠㅠㅠㅠㅠㅠㅠㅠㅠ 감사합니다유 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ
ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ
앞으로 더 추가해야 될게 많긴한데 이게 해결돼서 행복해요 ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ 감사합니당 ㅠㅠㅠㅠㅠㅠㅠㅠ
...