运行时错误表明没有这样的表

问题描述:

我创建数据库。二三个表的表成功创建,但第三个表是不created.It显示错误,因为没有这样的表运行时错误表明没有这样的表

数据库类是....

package com.example.mytryapp; 

import com.example.mytryapp.model.PendingDues; 
import com.example.mytryapp.model.Student; 
import com.example.mytryapp.model.Teacher; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class DatabaseHelper extends SQLiteOpenHelper { 

    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "behere.db"; 

    //table_teacher 
    private static final String TABLE_NAME_TEACHER = "teacher_table"; 
    //TEACHER_TABLE_COLUMNS 
    private static final String TEACHER_NAME = "teacher_name"; 
    private static final String TEACH_FATHER_NAME = "father_name"; 
    private static final String TEACH_MOTHER_NAME = "mother_name"; 
    private static final String TEACH_ADDRESS = "address"; 
    private static final String TEACH_CONTACT = "contact"; 
    private static final String TEACH_E_MAIL = "e_mail"; 
    private static final String TEACH_BRANCH = "branch"; 
    private static final String TEACH_EMP_ID = "_emp_id"; 

    //STUDENT_TABLE 
    private static final String TABLE_NAME_STUDENT="student_table"; 
    //STUDENT_TABLE_COLUMNS 
    private static final String ROLL_NO = "_roll_no"; 
    private static final String STUDENT_NAME = "student_name"; 
    private static final String STUDENT_FATHER_NAME = "student_father_name"; 
    private static final String STUDENT_MOTHER_NAME = "student_mother_name"; 
    private static final String STUDENT_ADDRESS = "student_address"; 
    private static final String STUDENT_CONTACT = "student_contact"; 
    private static final String STUDENT_E_MAIL = "student_e_mail"; 
    private static final String STUDENT_BRANCH_BATCH = "student_branch_batch"; 

    //pending_dues_teacher 
     private static final String TABLE_NAME_DUES = "pending_dues_table"; 
     //DUES_TABLE_COLUMNS 
     private static final String PENDING_DUES_NOTICE = "pending_dues_notice"; 
     private static final String PENDING_DUES_ROLLNO = "pending_dues_rollno"; 
     private static final String PENDING_DUES_ID = "_pending_dues_id"; 



    SQLiteDatabase data=this.getWritableDatabase(); 

    Context ctx; 

    public DatabaseHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     ctx=context; 
     Log.d("DATABASE OPERATION", "DATABASE CREATED"); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     try { 
      // Create table teacher 
      String create_table_teacher = "CREATE TABLE " + TABLE_NAME_TEACHER + " (" 
      + TEACH_EMP_ID + " INTEGER PRIMARY KEY," 
      + TEACHER_NAME + " TEXT," 
      + TEACH_FATHER_NAME + " TEXT," 
      + TEACH_MOTHER_NAME + " TEXT," 
      + TEACH_ADDRESS + " TEXT," 
      + TEACH_CONTACT + " TEXT," 
      + TEACH_E_MAIL + " TEXT," 
      + TEACH_BRANCH + " TEXT" 
      + ");"; 

      db.execSQL(create_table_teacher); 

      // Create table STUDENT 
      String CREATE_TABLE_student = "CREATE TABLE " + TABLE_NAME_STUDENT + " (" 
        + ROLL_NO + " INTEGER PRIMARY KEY," 
        + STUDENT_NAME + " TEXT," 
        + STUDENT_FATHER_NAME + " TEXT," 
        + STUDENT_MOTHER_NAME + " TEXT," 
        + STUDENT_BRANCH_BATCH + " TEXT," 
        + STUDENT_ADDRESS + " TEXT," 
        + STUDENT_CONTACT + " TEXT," 
        + STUDENT_E_MAIL + " TEXT" 
        + ");"; 

       db.execSQL(CREATE_TABLE_student);  

      // Create table pending dues 
       String create_table_pending_dues = "CREATE TABLE " + TABLE_NAME_DUES + " (" 
       + PENDING_DUES_ID + " INTEGER PRIMARY KEY," 
       + PENDING_DUES_ROLLNO + " TEXT," 
       + PENDING_DUES_NOTICE + " TEXT" 
       + ");"; 

       db.execSQL(create_table_pending_dues); 

     } catch (SQLException se) { 
      Log.v("DatabaseHandler Oncreate SQLException", 
        Log.getStackTraceString(se)); 
     } catch (Exception e) { 
      Log.v("DatabaseHandler Oncreate Exception", 
        Log.getStackTraceString(e)); 
     } 
      Log.d("database operation", "table created"); 
    } 


    public void open() throws SQLException 
    { 
     DatabaseHelper db1 = new DatabaseHelper(ctx); 
      data = db1.getWritableDatabase(); 
    } 

    public void close() 
    { 
     data.close(); 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     try { 
      // Drop table TEACHER 
         String placeTableDropQuery = "DROP TABLE IF EXISTS " + TABLE_NAME_TEACHER; 
         db.execSQL(placeTableDropQuery); 
         // Drop table STUDENT 
         String ReminderTableDropQuery = "DROP TABLE IF EXISTS " + TABLE_NAME_STUDENT; 
         db.execSQL(ReminderTableDropQuery); 
         // Drop table PENDING DUES 
         String pendingduesTableDropQuery = "DROP TABLE IF EXISTS " + TABLE_NAME_DUES; 
         db.execSQL(pendingduesTableDropQuery); 
         // Upgrade database 
         onCreate(db); 

        } catch (SQLException se) { 
         Log.v("DatabaseHandler onUpgrade SQLException", 
           Log.getStackTraceString(se)); 
        } catch (Exception e) { 
         Log.v("DatabaseHandler onUpgrade Exception", 
           Log.getStackTraceString(e)); 
        } 
    } 

    //add details of teacher 
    public String addTeacherData(Teacher teach) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     try { 
      ContentValues values = new ContentValues(); 
      values.put(TEACHER_NAME, teach.getTeacherName()); 
      values.put(TEACH_FATHER_NAME,teach.getTeacherFatherName()); 
      values.put(TEACH_MOTHER_NAME,teach.getTeacherMotherName()); 
      values.put(TEACH_ADDRESS,teach.getTeacherAddress()); 
      values.put(TEACH_CONTACT,teach.getTeacherContact()); 
      values.put(TEACH_E_MAIL,teach.getTeacherEmail()); 
      values.put(TEACH_BRANCH,teach.getTeacherBranch()); 

      db.insert(TABLE_NAME_TEACHER, null, values); 
      db.close(); 
      return "Record insert succussfully..."; 
     } catch (SQLiteException se) { 
      Log.v("DatabaseHelper insertTeacherRecord Exception", 
        Log.getStackTraceString(se)); 
      return se.getMessage(); 
     } catch (Exception e) { 
      Log.v("DatabaseHelper insertTeacherRecord Exception", 
        Log.getStackTraceString(e)); 
      return e.getMessage(); 
     } finally{ 
      db.close(); 
     } 
     } 
    //get teacher id in edit text 
    public Teacher findTeacherID(String teacher_id) { 
     String query = "Select * FROM " + TABLE_NAME_TEACHER + " WHERE " + TEACHER_NAME + " = \"" + teacher_id + "\""; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(query, null); 
     Teacher product = new Teacher(); 
     if (cursor.moveToFirst()) { 
      cursor.moveToFirst(); 
      product.setTeacherId(Integer.parseInt(cursor.getString(0))); 
      cursor.close(); 
     } else { 
      product = null; 
     } 
      db.close(); 
     return product; 
    } 
    //get teacher login id 
    public Cursor getInformation(DatabaseHelper dop) 
    { 
     SQLiteDatabase SQ=dop.getReadableDatabase(); 
     String[] coloumns={TEACH_EMP_ID}; 
     Cursor CR=SQ.query(TABLE_NAME_TEACHER, coloumns, null, null, null, null, null); 
     return CR;  
    } 
    //get student login id 
    public Cursor getStudentInformation(DatabaseHelper dop) 
    { 
     SQLiteDatabase SQ=dop.getReadableDatabase(); 
     String[] coloumns={ROLL_NO}; 
     Cursor CR=SQ.query(TABLE_NAME_STUDENT, coloumns, null, null, null, null, null); 
     return CR;  
    } 
    //add student details 
    public String addStudentData(Student student) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     try { 
      ContentValues values = new ContentValues(); 
      values.put(STUDENT_NAME, student.getStudentName()); 
      values.put(STUDENT_FATHER_NAME,student.getStudentFatherName()); 
      values.put(STUDENT_MOTHER_NAME,student.getStudentMotherName()); 
      values.put(STUDENT_BRANCH_BATCH,student.getStudentBranch()); 
      values.put(STUDENT_ADDRESS,student.getStudentAddress()); 
      values.put(STUDENT_CONTACT,student.getStudentContact()); 
      values.put(STUDENT_E_MAIL,student.getStudentEmail()); 

      db.insert(TABLE_NAME_STUDENT, null, values); 
      db.close(); 
      return "Record insert succussfully..."; 
     } catch (SQLiteException se) { 
      Log.v("DatabaseHelper insertTeacherRecord Exception", 
        Log.getStackTraceString(se)); 
      return se.getMessage(); 
     } catch (Exception e) { 
      Log.v("DatabaseHelper insertTeacherRecord Exception", 
        Log.getStackTraceString(e)); 
      return e.getMessage(); 
     } finally{ 
      db.close(); 
     }   
} 
    //get student id in edit text 
    public Student findStudentID(String student_id) { 
     String query = "Select * FROM " + TABLE_NAME_STUDENT + " WHERE " + STUDENT_NAME + " = \"" + student_id + "\""; 

     SQLiteDatabase db = this.getWritableDatabase(); 

     Cursor cursor = db.rawQuery(query, null); 

     Student student = new Student();   
     if (cursor.moveToFirst()) { 
      cursor.moveToFirst(); 
      student.setStudentId(Integer.parseInt(cursor.getString(0))); 
      cursor.close(); 
     } else { 
      student = null; 
     } 
      db.close(); 
     return student; 
    } 
    public Cursor getRollNo(DatabaseHelper dop) 
    { 
     SQLiteDatabase sq=dop.getReadableDatabase(); 
     String[] coloumns={ROLL_NO}; 
     Cursor cr=sq.query(TABLE_NAME_STUDENT, coloumns, null, null,null,null,null); 
     if (cr != null) { 
      cr.moveToFirst(); 
     } 
     return cr; 
    } 
    public Student findStudentDetail(int student_id) { 
     String query = "Select * FROM " + TABLE_NAME_STUDENT + " WHERE " + ROLL_NO + " = \"" + student_id + "\""; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(query, null); 
     Student product = new Student(); 
     if (cursor.moveToFirst()) { 
      cursor.moveToFirst(); 
      product.setStudentId(Integer.parseInt(cursor.getString(0))); 
      product.setStudentName(cursor.getString(1)); 
      product.setStudentFtherName(cursor.getString(2)); 
      product.setStudentMotherName(cursor.getString(3)); 
      product.setStudentBranch(cursor.getString(4)); 
      cursor.close(); 
     } else { 
      product = null; 
     } 
      db.close(); 
     return product; 
    } 
    //add details of teacher 
     public String addDuesData(PendingDues dues) { 
      SQLiteDatabase db = this.getWritableDatabase(); 
      try { 
       ContentValues values = new ContentValues(); 
       values.put(PENDING_DUES_ROLLNO, dues.getDueNoticeRollNo()); 
       values.put(PENDING_DUES_NOTICE,dues.getDueNoticeName()); 
       db.insert(TABLE_NAME_DUES, null, values); 
       db.close(); 
       return "Record insert succussfully..."; 
      } catch (SQLiteException se) { 
       Log.v("DatabaseHelper insertTeacherRecord Exception", 
         Log.getStackTraceString(se)); 
       return se.getMessage(); 
      } catch (Exception e) { 
       Log.v("DatabaseHelper insertTeacherRecord Exception", 
         Log.getStackTraceString(e)); 
       return e.getMessage(); 
      } finally{ 
       db.close(); 
      } 
      } 

} 

活动....

package com.example.mytryapp; 

import com.example.mytryapp.model.PendingDues; 
import com.example.mytryapp.model.Student; 
import com.example.mytryapp.model.Teacher; 

import android.app.ActionBar; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class ManagePendingDue extends Activity{ 

    TextView TXT1,TXT2,TXT3,TXT4,TXT5; 
    String rollno, due_notice,i; 
    EditText DUE_NOTICE,GET_ROLLNO_DUES; 
    Button send; 
    SQLiteDatabase db; 
Context ctxx=this; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     ActionBar ab=getActionBar(); 
     ab.setDisplayUseLogoEnabled(false); 
      ab.setDisplayHomeAsUpEnabled(true); 
      ab.setDisplayShowHomeEnabled(false); 
     setContentView(com.example.mytryapp.R.layout.manage_pending_dues); 
     DatabaseHelper DB =new DatabaseHelper(ManagePendingDue.this); 
     TXT1=(TextView)findViewById(R.id.text_rollno); 
     TXT2=(TextView)findViewById(R.id.text_name); 
     TXT3=(TextView)findViewById(R.id.text_father); 
     TXT4=(TextView)findViewById(R.id.text_mother); 
     TXT5=(TextView)findViewById(R.id.text_branch); 
     DUE_NOTICE=(EditText)findViewById(R.id.edit_due_notice); 
     GET_ROLLNO_DUES=(EditText)findViewById(R.id.edit_rollno_enter); 
     send=(Button)findViewById(R.id.button_send); 

     ManageDues m=new ManageDues(); 

     Bundle b=this.getIntent().getExtras(); 
    i = b.getString("userdata"); 
    int ii=Integer.parseInt(i); 

     Student stud = DB.findStudentDetail(ii); 
      if (stud != null) { 
       TXT1.setText(String.valueOf(stud.getStudentId())); 
       TXT2.setText(String.valueOf(stud.getStudentName())); 
       TXT3.setText(String.valueOf(stud.getStudentFatherName())); 
       TXT4.setText(String.valueOf(stud.getStudentMotherName())); 
       TXT5.setText(String.valueOf(stud.getStudentBranch()));   
      }  
      send.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       GET_ROLLNO_DUES.setText(i); 
       due_notice=DUE_NOTICE.getText().toString(); 

       DatabaseHelper DB=new DatabaseHelper(ctxx); 
       DB.open(); 
       PendingDues dues=new PendingDues(i,due_notice); 
       DB.addDuesData(dues); 
      Toast.makeText(getBaseContext(), "insertion sucessful", Toast.LENGTH_LONG).show();    
      } 
     }); 
}  
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater1 = getMenuInflater(); 
     inflater1.inflate(R.menu.main, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle presses on the action bar items 
     switch (item.getItemId()) { 
      case R.id.logout: 

       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setMessage("Do you want to logout ?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

         Intent std_time=new Intent(ManagePendingDue.this,ChoosePanel.class); 
         startActivity(new Intent(ManagePendingDue.this,ChoosePanel.class)); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
        // Action for 'NO' Button 
        dialog.cancel(); 
        } 
       }); 

      //Creating dialog box 
      AlertDialog alert = builder.create(); 
      //Setting the title manually 
      alert.setTitle("LOGOUT"); 
      alert.show(); 
       return true; 

      case android.R.id.home: 
       // app icon in action bar clicked; goto parent activity. 
       this.finish(); 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
      } 
     } 
} 

和错误的logcat是...

05-20 05:14:54.094: E/SQLiteLog(820): (1) no such table: pending_dues_table 
05-20 05:14:54.244: E/SQLiteDatabase(820): Error inserting pending_dues_rollno=1 pending_dues_notice=pending fees 2500rs 
05-20 05:14:54.244: E/SQLiteDatabase(820): android.database.sqlite.SQLiteException: no such table: pending_dues_table (code 1): , while compiling: INSERT INTO pending_dues_table(pending_dues_rollno,pending_dues_notice) VALUES (?,?) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at com.example.mytryapp.DatabaseHelper.addDuesData(DatabaseHelper.java:290) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at com.example.mytryapp.ManagePendingDue$1.onClick(ManagePendingDue.java:79) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.view.View.performClick(View.java:4240) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.view.View$PerformClick.run(View.java:17721) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.os.Handler.handleCallback(Handler.java:730) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.os.Looper.loop(Looper.java:137) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at android.app.ActivityThread.main(ActivityThread.java:5103) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at java.lang.reflect.Method.invokeNative(Native Method) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at java.lang.reflect.Method.invoke(Method.java:525) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
05-20 05:14:54.244: E/SQLiteDatabase(820): at dalvik.system.NativeStart.main(Native Method) 
05-20 05:14:54.294: I/Choreographer(820): Skipped 114 frames! The application may be doing too much work on its main thread. 
+0

你在哪里插入数据到'pending_dues_table'表? YOu正在尝试插入数据,因为错误状态 – hrskrs

+0

在ManagePendingDue活动中,我正在插入数据.....在发送按钮上点击监听器时出现此错误 –

更新数据库版本2。请注意,在第一次执行此操作时,您将丢失表中的所有数据,因为您要删除onUpgrade(db, oldVersion, newVersion)中的表。

private static final int DATABASE_VERSION = 2;

编辑: 你想改变你的表结构时,都会被称为onUpgrade(db, oldVersion, newVersion)需求。要调用它,您需要更新数据库版本。

编辑2: 卸载并重新安装该应用程序还可以帮助,但是这是说在这种情况下,从Settings > Apps“清除数据”的另一种方式,它不会帮助你理解这个问题到底是什么。

+0

感谢它的工作......请帮助我更多,如果我正在创建在这个数据库类中更多两个或三个表,所以它必须将数据库的版本从2增加到3? –

+0

@MonikaKrail检查我的编辑:) – Karim

+1

意味着如果我改变我的表,那么我需要每次更改数据库的版本.... –

如果您稍后添加第三个表格,则需要卸载应用程序并重新安装一次,或者必须增加数据库版本。

+0

感谢它的工作......请帮助我更多,如果我在这个数据库类创建更多的两个或三个表,所以它必须增加数据库的版本从2到3? –

+0

如果您在市场上发布了第一版应用程序,并且在第二版应用程序中添加了另一个表格,则必须增加数据库版本,以便onUpgrade方法将被调用并且您的数据库将自动刷新以供您的应用程序使用用户。如果应用程序未发布,则可以卸载旧版本并运行您的应用程序。 –

+0

好的意思是,如果我现在不发布我的应用程序,那么我只是卸载这个,这将正常工作@阿伦库马尔谢谢:) –