如何在android中为我的数据库添加信息?

时间:2017-08-31 08:49:47

标签: android sqlite

我正在写一个关于电影的节目,我需要创建一个数据库。所以我做到了。当我想要在我的列表中添加电影时运行应用程序后没有任何事情发生。这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rigister_series);
    setTitle("Register series");
    Button btnsubmit;
    final EditText moviename,movieyear,moviegenre,directername;
    TextView textViewcount;
    final database_manager db = new database_manager(this);
    int result=db.getmoviecount();
    moviename=(EditText)findViewById(R.id.moviename);
    movieyear=(EditText)findViewById(R.id.movieyear);
    moviegenre=(EditText)findViewById(R.id.moviegenere);
    directername=(EditText)findViewById(R.id.directername);
    textViewcount=(TextView)findViewById(R.id.textViewcount);
    textViewcount.setText(Integer.toString(result));
    btnsubmit=(Button)findViewById(R.id.btnsubmit);
    btnsubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          String Name=moviename.getText().toString();
            String Genre=moviegenre.getText().toString();
            int Year = Integer.parseInt(movieyear.getText().toString());
            String Directername=directername.getText().toString();
            movies_item movieslist=new movies_item();
            movieslist.movie_name=Name;
            movieslist.movie_year=Year;
            movieslist.movie_genre=Genre;
            movieslist.directer_name=Directername;
            Boolean result= db.addmovie(movieslist);
            if(result){
                toastshow();
            }else


Toast.makeText(getApplicationContext(),"Failed!",Toast.LENGTH_SHORT).show();

        }
    });
}

 private void toastshow() {
    Context context= getApplicationContext();
    LayoutInflater layoutinfalter= getLayoutInflater();
    View view= layoutinfalter.inflate(R.layout.custom_toast,null);
    Toast toast = new Toast(context);
    toast.setView(view);
    toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.show();

}

还有我的数据库经理:

 public database_manager(Context context) {
    super(context,"mydb" , null,1);

}

@Override
public void onCreate(SQLiteDatabase db) {
    String query= " CREATE TABLE tbl_movies (" +
    "  id     INT    PRIMARY   KEY     AUTOINCREMENT   " +
                      "    NOT    NULL    " +
                      "   UNIQUE  ,  " +

    "  name              VARCHAR   NOT   NULL   " +
                       "   UNIQUE  ,  " +
    "  year               NUMERIC   ,   " +
    "  genre              VARCHAR   ,  "  +
    "  directer   VARCHAR    " +
    "  );  ";

          db.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
     db.execSQL("DROP TABLE IF EXISTS tbl_movies");
    onCreate(db);
}

public List<movies_item> getmoviesitem(){

    List<movies_item> List_movies=new ArrayList<movies_item>();
    String query="SELECT
 movie_id,movie_name,directer_name,movie_year,movie_genre FROM tbl_Hesab";  
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cursor=db.rawQuery(query, null);
    while(cursor.moveToNext()==true){
        movies_item h= new movies_item();
        h.movie_id=cursor.getInt(0);
        h.movie_name=cursor.getString(1);
        h.movie_year=cursor.getInt(2);
        h.movie_genre=cursor.getString(3);
        h.directer_name=cursor.getString(4);

        List_movies.add(h);
    }
    return List_movies;
}


public boolean addmovie (String movie_name,int movie_year, String 
 movie_genre, String directer_name){
     boolean result;
    String query="INSERT INTO tbl_movies(name,year,genre,directer ) " +
            " VALUES   
 ('"+movie_name+"',"+movie_year+",'"+movie_genre+"','"+directer_name+"') ";
    try {

        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(query);
        db.close();
        result=true;
    }
     catch (Exception ex){
         result=false;

     }
    return result;
}
public boolean updatemovie(movies_item moviesItem){
    boolean result;
    try {
        String query="UPDATE tbl_movies SET name='"+ movie_name+"',year="+  
 movie_year+",genre='"+ movie_genre+"',directer ='"+ directer_name+"'"+
                "where id="+ movie_id+"";
        SQLiteDatabase db=this.getWritableDatabase();
        db.execSQL(query);
        db.close();
        result=true;
    }
    catch (Exception ex){
        result=false;
    }
  return result;

}
public movies_item getmovie(int movie_id) {
movies_item movies_item= new movies_item();
    String query= "SELECT * FROM tbl_movies  WHERE id="+ movie_id;
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cursor=db.rawQuery(query,null);
    if (cursor.moveToFirst()==true){
        movie_name=cursor.getString(1);
        movie_year=cursor.getInt(2);
        movie_genre=cursor.getString(3);
        directer_name=cursor.getString(4);
    }
    return  movies_item;

}
public int getmoviecount (){
    int result=0;
    String query ="SELECT * FROM tbl_movies ";
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query,null);
    result=cursor.getCount();
    db.close();
    return  result;
}

 public boolean deletemovie(){
 boolean result;
 try {
    String query= "DELETE FROM tbl_movies WHERE id="+movie_id;
    SQLiteDatabase db= this.getWritableDatabase();
    db.execSQL(query);
    db.close();
    result=true;
}
catch (Exception ex){
    result=false;
}
 return  result;

}

public Boolean addmovie(movies_item moviesItem) {
    boolean result;
    try {
        String query = "INSERT INTO tbl_movies(name,year,genre,directer ) " +
                " VALUES ('" + movies_item.movie_name + "',"   
 +movies_item.movie_year + ",'" + movies_item.movie_genre + "','" 
 +movies_item.directer_name + "') ";
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(query);
        db.close();
        result = true;
    } catch (Exception ex) {
        result = false;

    }
    return result;
  }
}

我只是继续接收失败的消息,这意味着没有任何反应。 谁能帮助我?

1 个答案:

答案 0 :(得分:-1)

您的数据库最后需要.db,例如mydb.db

如果您使用SqliteOpenHelper,则可以调用。

db.insert(String tableName, String nullColumnHack, ContentValues values)

您需要做的就是将每个列名称和值添加到ContentValues对象。