السلام عليكم
عندي داتابيس في الاندوريد
عملت جدول
لكن لما ابغي اضيف صف اخر في الجدول
يعني مابيقبل الاضافه مره اخرى
مع اني ما عملت unique عدم التكرار
||
هذا كلاس الداتابيس :
package com.example.tests; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import java.util.ArrayList; public class Mydatabase extends SQLiteOpenHelper { public static final String Db_name="Firstdatabase"; public static final String COLOR_CMD="COLOR"; public static final String CAR_TB_NAME="CAR"; public static final String MODEL_CMD="MODEL"; public static final String ID_CMD="ID"; public static final String DISTANCE_CMD="DISTANCE"; public static final int Db_version=7; public Mydatabase(Context context){ super(context,Db_name,null,Db_version); } @Override //يتم استدعاء الداله عند انشاء الداتابيس public void onCreate(SQLiteDatabase db) { //CREATE NEW TABLE //طريقة انشاء جدول في الداتابيس db.execSQL("CREATE TABLE "+CAR_TB_NAME+"("+ID_CMD+" INTEGER PRIMARY KEY AUTOINCREMENT ,"+MODEL_CMD+" TEXT ,"+COLOR_CMD+" TEXT NOT NULL ,"+DISTANCE_CMD+" REAL)"); } @Override //يتم استدعاء الداله عند تحديث داتا بيس public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " +CAR_TB_NAME ); onCreate(db); } public boolean insertdata(Car car){ SQLiteDatabase db=getWritableDatabase(); ContentValues values=new ContentValues(); values.put(ID_CMD,car.getId()); values.put(MODEL_CMD,car.getModel()); values.put(COLOR_CMD,car.getColor()); values.put(DISTANCE_CMD,car.getDistance()); long result=db.insert(CAR_TB_NAME,null,values); if(result==-1){ return false; }else return true; } //دالة التعديل public boolean updatedata(Car car){ SQLiteDatabase db=getWritableDatabase(); ContentValues values=new ContentValues(); values.put(ID_CMD,car.getId()); values.put(MODEL_CMD,car.getModel()); values.put(COLOR_CMD,car.getColor()); values.put(DISTANCE_CMD,car.getDistance()); String [] args={car.getId()+""}; int result=db.update(CAR_TB_NAME,values,"id?",args); return result>0; } // دالة الحدف public boolean deletedata(Car car){ SQLiteDatabase db=getWritableDatabase(); String [] args={car.getId()+""}; int result=db.delete(CAR_TB_NAME,"id?",args); return result>0; } //ارجاع عدد الصفوف الجدول public long getcarscount(){ SQLiteDatabase db=getWritableDatabase(); return DatabaseUtils.queryNumEntries(db,CAR_TB_NAME); } public ArrayList<Car> getallcars(){ ArrayList<Car> cars=new ArrayList<Car>(); SQLiteDatabase db=getReadableDatabase(); Cursor c= db.rawQuery("SELECT * FROM " +CAR_TB_NAME,null ); if(c.moveToFirst()){ do { int id=c.getInt(c.getColumnIndex(ID_CMD)); String model=c.getString(c.getColumnIndex(MODEL_CMD)); String color=c.getString(c.getColumnIndex(COLOR_CMD)); double distance=c.getDouble(c.getColumnIndex(DISTANCE_CMD)); Car car=new Car(id,color,model,distance); cars.add(car); } while(c.moveToNext()); c.close(); } return cars; } public ArrayList<Car> SEARCHdata(String modelsearch){ ArrayList<Car> cars=new ArrayList<Car>(); SQLiteDatabase db=getReadableDatabase(); Cursor c= db.rawQuery("SELECT * FROM " +CAR_TB_NAME+" WHERE "+ MODEL_CMD+"=?",new String[] {modelsearch} ); if(c.moveToFirst()){ do { int id=c.getInt(c.getColumnIndex(ID_CMD)); String model=c.getString(c.getColumnIndex(MODEL_CMD)); String color=c.getString(c.getColumnIndex(COLOR_CMD)); double distance=c.getDouble(c.getColumnIndex(DISTANCE_CMD)); Car car=new Car(id,color,model,distance); cars.add(car); } while(c.moveToNext()); c.close(); } return cars; } }
والاستدعاء هنا:
package com.example.tests; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import android.Manifest; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.google.android.material.snackbar.Snackbar; import org.w3c.dom.Text; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { SharedPreferences sp; SharedPreferences.Editor editor; static EditText et_distance; static EditText et_color,et_model; Button save, restore,delete; TextView textView; Mydatabase mydatabase; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_distance = findViewById(R.id.editTextTextPersonName3); et_color = findViewById(R.id.editTextTextPersonName4); et_model = findViewById(R.id.editTextTextPersonName); save = findViewById(R.id.save_btn); restore = findViewById(R.id.srestore_btn2); textView = findViewById(R.id.textView); delete=findViewById(R.id.delete); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); mydatabase = new Mydatabase(this); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { String model = et_model.getText().toString(); String color = et_color.getText().toString(); double dis = Double.parseDouble(et_distance.getText().toString()); Car car=new Car(color,model,dis); boolean res=mydatabase.insertdata(car); if(res) Snackbar.make(textView,"تمت العمليه بنجاح",Snackbar.LENGTH_LONG).show(); else Snackbar.make(textView," فشلت العمليه ",Snackbar.LENGTH_LONG).show(); } catch (Exception e){ } } }); restore.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ArrayList<Car> cars=mydatabase.getallcars(); for(Car c: cars){ Log.d("car #"+c.getId(),c.getModel()); Log.d("car #"+c.getId(),c.getColor()); } } }); } }
لما اضيف مرة اخرى بيقول فشلت العمليه بيعني بيرجع false
هل في شئ نسيته في الكود؟
التعليقات