vendredi 8 janvier 2016

no such table in android external database

this is my code , but no such table error in listing of my dadtabase value or inserting , cannot connect to my database and cannot find my table

this is my assetdb code :

package com.paragraph.paragraphdoc;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.util.Log;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* Created by HP-HP on 23-07-2015.
*/
public class Assetdb extends SQLiteOpenHelper {

//The Android's default system path of your application database.
private static String DB_PATH ;

private static String DB_NAME = "pargraph";

private SQLiteDatabase myDataBase;

private final Context myContext;

/**
 * Constructor
 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
 * @param context
 */
public Assetdb(Context context) {

    super(context, DB_NAME, null, 1);

    if(android.os.Build.VERSION.SDK_INT >= 4.2){
        DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
    } else {
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }

    this.myContext = context;
}

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */
public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if(dbExist){
        //do nothing - database already exist
    }else{

        //By calling this method and empty database will be created into the default system path
        //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){

        //database does't exist yet.
        try {
            myContext.deleteDatabase("pargraph");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException {

    //Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.CREATE_IF_NECESSARY );

}

@Override
public synchronized void close() {

    if(myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.


    public boolean inserInTblInfo(String reshteh,String docname,String ostad,String storage,String pagenumber, String price,String desc){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put("reshteh", reshteh);
        cv.put("docname", docname);
        cv.put("ostad", ostad);
        cv.put("storage", storage);
        cv.put("pagenumber", pagenumber);
        cv.put("price", price);
        cv.put("desc", desc);
        db.insert("ptable", null, cv);
        Log.e("???????????", "insert shod"); 
        return true;
    }
    public ArrayList getAll(){
        ArrayList arr1=new ArrayList();
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cur=db.rawQuery("select * from ptable", null);
        cur.moveToFirst();
        while(cur.isAfterLast()==false){
            arr1.add(cur.getString(cur.getColumnIndex("docname")));
            cur.moveToNext();
        }
        return arr1;
    }


//  public int getProfilesCount() {
//      String countQuery = "SELECT  * FROM doctbl";
//      SQLiteDatabase db = this.getReadableDatabase();
//      Cursor cursor = db.rawQuery(countQuery, null);
//      int cnt = cursor.getCount();
//      cursor.close();
//      return cnt;
//  }

}

and this is my main activity code :

package com.paragraph.paragraphdoc;

import java.util.ArrayList;

import com.paragraph.paragraphdoc.Assetdb;
import com.paragraph.paragraphdoc.R;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Spinner;


public class MainActivity extends ActionBarActivity {
    private String array_spinner[]=new String[23];
    private ImageButton add;
    private ListView lv_main;
    private EditText search;
    Assetdb dbShowALL;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner s = (Spinner) findViewById(R.id.spineer_reshteh);
        ImageButton add=(ImageButton) findViewById(R.id.imgbtn_add);
        ListView lv_main=(ListView)findViewById(R.id.lv_main);
        EditText search=(EditText)findViewById(R.id.editText_search);

        dbShowALL=new Assetdb(this);
        ArrayList arr1=dbShowALL.getAll();
        ArrayAdapter arrAdapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1,arr1);
        lv_main.setAdapter(arrAdapter);



        add.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                Intent nextpage=new Intent(MainActivity.this,Reg.class);
                startActivity(nextpage);

            }

        });

        spineer(s);

    }

Aucun commentaire:

Enregistrer un commentaire