运行时错误显示没有这样的表

时间:2015-05-20 09:28:47

标签: android

我在数据库中创建了三个表。成功创建了两个表,但没有创建第三个表。它显示错误没有这样的表

数据库类是....

((void (*)())0x1000)();

活动是......

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();
            } 
            }

}

并且logcat中的错误是......

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);
            }
        }
}

2 个答案:

答案 0 :(得分:1)

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

private static final int DATABASE_VERSION = 2;

修改 每次要更改表的结构时,都需要调用onUpgrade(db, oldVersion, newVersion)。要调用它,您需要更新数据库版本。

编辑2: 卸载并重新安装应用程序也有帮助,但这是另一种说法"清除数据"在这种情况下,来自Settings > Apps,它无法帮助您准确理解问题所在。

答案 1 :(得分:1)

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