سلام
من داخل اپ خودم برای سبد خرید از پایگاه داده sqlite استفاده کردم . فایل رو ساختم و داخل پوشه asset قرار دادم ولی برنامه هنگام اجرا وقتی که میزنم روی سبد خرید میندازه بیرون و از Logcat فهمیدم که نمیتونه پایگاه داده رو باز کنه . الان دو روزه درگیرشم . اگه میشه کمک کنین . ممنون . (کد هایی که خطا میگیره رو قرمز میکنم) در ضمن خودم هم به یه چیزی شک دارم که عکسشو در انتها قرار میدم
کد های Database.java:
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
import com.shm_rz.onlinefoodapp.Model.Order;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Roozbeh Zamani on 16/03/2018.
*/
public class Database extends SQLiteAssetHelper {
private static final String DB_NAME = "OnlineFoodApp" ;
private static final int DB_VER = 1 ;
public Database(Context context) {
super(context, DB_NAME, null, DB_VER);
}
public List<Order> getCarts()
{
SQLiteDatabase db = this.getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = {"ProductId","ProductName","Quantity","Price","Discount"};
String sqlTable = "orderDetail";
qb.setTables(sqlTable);
Cursor c = qb.query(db, sqlSelect, null, null, null, null, null);
final List<Order> result = new ArrayList<>();
if(c.moveToFirst())
{
do{
result.add(
new Order(c.getString(c.getColumnIndex("ProductId")),
c.getString(c.getColumnIndex("ProductName")),
c.getString(c.getColumnIndex("Quantity")),
c.getString(c.getColumnIndex("Price")),
c.getString(c.getColumnIndex("Discount"))
));
}while (c.moveToNext());
}
return result;
}
public void addToCart(Order order)
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("INSERT INTO orderDetail(ProductId,ProductName,Quantity,Price,Discount) VALUES ('%s', '%s', '%s', '%s', '%s');",
order.getProductId(),
order.getProductName(),
order.getQuantity(),
order.getPrice(),
order.getDiscount());
db.execSQL(query);
}
public void cleanCart()
{
SQLiteDatabase db = getReadableDatabase();
String query = String.format("DELETE FROM orderDetail");
db.execSQL(query);
}
}
کدهای Cart.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.shm_rz.onlinefoodapp.Database.Database;
import com.shm_rz.onlinefoodapp.Model.Order;
import com.shm_rz.onlinefoodapp.ViewHolder.CartAdapter;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import ru.katso.livebutton.LiveButton;
public class Cart extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference request;
TextView txtTotalPrice;
LiveButton btnPlace;
List<Order> cart = new ArrayList<>();
CartAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
//firebase
database = FirebaseDatabase.getInstance();
request = database.getReference("Requests");
//init
recyclerView = (RecyclerView) findViewById(R.id.listCart);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
txtTotalPrice = (TextView) findViewById(R.id.total);
btnPlace = (LiveButton) findViewById(R.id.btnPlaceOrder);
loadListFood();
}
private void loadListFood() {
cart = new Database(this).getCarts();
adapter = new CartAdapter(cart, this);
recyclerView.setAdapter(adapter);
//calculate total price
int total = 0;
for(Order order:cart)
total += (Integer.parseInt(order.getPrice())) * (Integer.parseInt(order.getQuantity()));
Locale locale = new Locale("en","US");
NumberFormat fmp = NumberFormat.getNumberInstance(locale);
txtTotalPrice.setText(fmp.format(total));
}
}
کد های activity_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="@drawable/register"
tools:context="com.shm_rz.onlinefoodapp.Cart">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listCart"
android:background="@color/fbutton_color_transparent"/>
<android.support.v7.widget.CardView
android:layout_alignParentBottom="true"
app:cardBackgroundColor="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="100dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_margin="8dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Total:"
android:textSize="20sp"
android:textColor="@android:color/white"/>
<TextView
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="$10,0000"
android:textSize="24sp"
android:textColor="@android:color/white"/>
</LinearLayout>
<ru.katso.livebutton.LiveButton
xmlns:livebutton="http://schemas.android.com/apk/res-auto"
android:id="@+id/btnPlaceOrder"
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_alignParentBottom="true"
android:layout_margin="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Place Order"
android:textColor="@android:color/white"
livebutton:backgroundColor="@color/btnSignActivity"
livebutton:cornerRadius="4dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
اینم از logcat
03-17 18:43:00.656 4410-4410/com.shm_rz.onlinefoodapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shm_rz.onlinefoodapp, PID: 4410
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shm_rz.onlinefoodapp/com.shm_rz.onlinefoodapp.Cart}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:264)
at com.shm_rz.onlinefoodapp.Database.Database.getCarts(Database.java:26)
at com.shm_rz.onlinefoodapp.Cart.loadListFood(Cart.java:61)
at com.shm_rz.onlinefoodapp.Cart.onCreate(Cart.java:57)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
من بعد از این که فایل پایگاه دادمو (.db) توی اندروید ستودیو باز میکنم ، اینجوری نشون میده
کاراکترهایی که استفاده کردید داخل اندروید استادیو تعریف نشده برای همین به صورت شکل نشون میده
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)