0
我寫一個關於電影的計劃,我需要建立一個數據庫。所以我做到了。並在運行應用程序時,當我想添加一個電影到我的列表沒有發生。這裏是我的代碼:我如何能在Android的信息添加到我的DATABSE?
@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;
}
}
我還是想繼續receving失敗的消息,這意味着沒有任何反應。 任何人都可以幫助我嗎?
使用Android活躍或SugarORM是如此簡單和快速,這兩種ActiveAndroid和SugarORm在內部工作sqlite的,但是在這些圖書館的容易創建表並保存在它 –
你有沒有在清單中添加的權限的數據?發佈你的錯誤。 – KCN
它在結果顯示什麼烤麪包? –