无法在android

时间:2016-04-08 07:29:50

标签: java android

我在student.db课程中创建了一个名为Student_profile的数据库和两个表Teacher_profileDatabaseHelper。当我将数据插入第一个表时,它会被插入但不会在我尝试使用第二个表时插入。 我确定在从我用来插入数据的活动的EditText字段中识别值时,我没有犯任何错误。 一个特别需要注意的事情是,如果我插入第二个表Teacher_profile的条目,调用它插入的第一个表insertData1的{​​{1}}函数(两个表具有相同类型的列和相同的列数)但是当我调用它的合法Student_profile函数时,我的问题确实存在。

我使用了三个java文件:

  1. MainActivity - >插入student_profile表

  2. 老师 - >插入teacher_profile表

  3. DatabaseHelper - >用于插入数据库的代码的java代码。

  4. insertData2
    package com.example.point.sqlite;
    
    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class DatabaseHelperClass extends SQLiteOpenHelper {
    
    public static final String Database_name="student.db";
    
    public static final String Table_name="Student_profile";
    public static final String col1="id";
    public static final String col2="name";
    public static final String col3="surname";
    public static final String col4="marks";
    
    public static final String Table_name2="Teacher_profile";
    public static final String cl1="id";
    public static final String cl2="name";
    public static final String cl3="surname";
    public static final String cl4="age";
    
    
    
    
    public DatabaseHelperClass(Context context) {
        super(context, Database_name, null, 1);//whenever the constructor will be called the database will be created
    }
    
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+ Table_name+"(id integer primary key autoincrement,name text,surname text,marks integer)");
        db.execSQL("create table "+ Table_name2+"(id integer primary key autoincrement,name text,surname text,age integer)");
    
    
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        db.execSQL("drop table"+Table_name);
        db.execSQL("drop table"+Table_name2);
    
        onCreate(db);
    }
    //for inserting the student records
    public boolean insertData(String name,String surname,String marks){
    
        SQLiteDatabase db=this.getWritableDatabase();
    
        ContentValues cont=new ContentValues();
        cont.put(col2,name);
        cont.put(col3, surname);
        cont.put(col4,marks);
        long result=db.insert(Table_name,null,cont);
        if(result==-1) {
            return false;
        }
        else
            return true;
    
    }
    // for showing student records
    public Cursor showDatabase()
    {
        SQLiteDatabase db=this.getWritableDatabase();
        Cursor res=db.rawQuery("select * from "+ Table_name,null);
        return  res;
    }
    //for inserting teacher record
    public boolean insertData2(String name,String surname,String age){
    
        SQLiteDatabase db=this.getWritableDatabase();
    
        ContentValues cont=new ContentValues();
        cont.put(col2,name);
        cont.put(col3, surname);
        cont.put(col4,age);
        long result=db.insert(Table_name2,null,cont);
        if(result==-1) {
            return false;
        }
        else
            return true;
    
    }
    //for showing teacher record
    public Cursor showDatabase2()
    {
        SQLiteDatabase db=this.getWritableDatabase();
        Cursor res=db.rawQuery("select * from "+ Table_name2,null);
        return  res;
    }
    public boolean updateDatabaseTable(String id,String name,String surname,String marks){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cont=new ContentValues();
        cont.put(col1,id);
        cont.put(col2,name);
        cont.put(col3, surname);
        cont.put(col4,marks);
        db.update(Table_name,cont,"id = ?",new String[] {id});//id is the parameter
        return true;
    
    }
    public int deleteData(String id)
    {
        SQLiteDatabase db=this.getWritableDatabase();
    
        return db.delete(Table_name,"id = ?",new String[] {id}); }}
    
    package com.example.point.sqlite;
    
    import android.app.Activity;
    
    import android.app.AlertDialog;
    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import.android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    
    public class MainActivity extends Activity {
    
    DatabaseHelperClass myDb;
    EditText ed1,ed2,ed3,id;
    Button b;//button to insert into student table
    Button teacher,showData,updateData,delData;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb=new DatabaseHelperClass(this);
        ed1=(EditText)findViewById(R.id.editText);//name
        ed2=(EditText)findViewById(R.id.editText2);//surname
        ed3=(EditText)findViewById(R.id.editText4);//marks
        b=(Button)findViewById(R.id.button);//insert button for student
        teacher=(Button)findViewById(R.id.button5);//button to go to teacher.java
        showData=(Button)findViewById(R.id.button2);
        updateData=(Button)findViewById(R.id.button3);
        delData=(Button)findViewById(R.id.button4);
        id=(EditText)findViewById(R.id.editText3);//id
    //this works well and data get inserted 
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isInserted=myDb.insertData(ed1.getText().toString(), ed2.getText().toString(), ed3.getText().toString());
                if(isInserted==true)
                {
                    Toast.makeText(getApplicationContext(),"data inserted",Toast.LENGTH_LONG).show();
                }
                else
                    Toast.makeText(getApplicationContext(),"data not inserted",Toast.LENGTH_LONG).show();
                setEmpty();
    
    
            }
        });
    
    
    
        teacher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(getApplicationContext(),Teacher.class);
                startActivity(i);
            }
        });
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                boolean res= myDb.updateDatabaseTable(id.getText().toString(),ed1.getText().toString(),ed2.getText().toString(),ed3.getText().toString());
                if(res==true)
                {
                    Toast.makeText(getApplicationContext(),"data updated",Toast.LENGTH_LONG).show();
    
                }
                else
                    Toast.makeText(getApplicationContext(),"data not updated",Toast.LENGTH_LONG).show();
    
              setEmpty();
            }
        });
    
    
    
    
    
        showData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor res = myDb.showDatabase();
                if (res.getCount() == 0)
                {
                    showMessage("Error!", "no data found");
                    return;
                }
                StringBuffer bf = new StringBuffer();
                while (res.moveToNext())
                {
    
                    bf.append("ID: " + res.getString(0)+"\n");//0 for first column that is col1 id
                    bf.append("NAME: " + res.getString(1)+"\n");
                    bf.append("SURNAME: " + res.getString(2)+"\n");
                    bf.append("MARKS: " + res.getString(3)+"\n\n");
                }
                showMessage("Data",bf.toString());
    
                setEmpty();
            }
    
        });
    
    
    
        delData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                int deletedRows=myDb.deleteData(id.getText().toString());
                if(deletedRows>0)
                    Toast.makeText(getApplicationContext(),"data deleted",Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(getApplicationContext(),"data not deleted",Toast.LENGTH_LONG).show();
    
                setEmpty();
    
    
            }
    
        });
    
    
    }
    
    public void setEmpty()
    {
        ed1.setText("");
        ed2.setText("");
        ed3.setText("");
        id.setText("");
    }
    public void showMessage(String title,String message)
    {
        AlertDialog.Builder build=new AlertDialog.Builder(this);
    
        build.setCancelable(true);
        build.setTitle(title);
        build.setMessage(message);
        build.show();
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
    
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    }`
    

2 个答案:

答案 0 :(得分:0)

检查您的第二个表格插入方法insertData2,并使用列名cl2代替col2,并使用相同的其他

答案 1 :(得分:0)

你必须使用..

public boolean insertData2(String name,String surname,String age){

    SQLiteDatabase db=this.getWritableDatabase();

    ContentValues cont=new ContentValues();
    cont.put(cl2,name);
    cont.put(cl3, surname);
    cont.put(cl4,age);
    long result=db.insert(Table_name2,null,cont);
    if(result==-1) {
        return false;
    }
    else
        return true;

}

而不是

public boolean insertData2(String name,String surname,String age){

    SQLiteDatabase db=this.getWritableDatabase();

    ContentValues cont=new ContentValues();
    cont.put(col2,name);
    cont.put(col3, surname);
    cont.put(col4,age);
    long result=db.insert(Table_name2,null,cont);
    if(result==-1) {
        return false;
    }
    else
        return true;

}