Dışarıdan Sqlite ile veri çekerken sıkıntı yaşıyorum(yardım)
19.01.2016 - 03:18
Dışarıdan oluşturuduğum veritabanını listview de yazdırmak istiyorum, E/SQLiteLog: (1) no such table:student hatası alıyorum.
public class DatabaseHelper extends SQLiteOpenHelper
{
static String DB_PATH;
//Veritabanı ismini veriyoruz
static String DB_NAME = "School";
SQLiteDatabase myDatabase;
final Context myContext;
public DatabaseHelper(Context context)
{
super(context, DB_NAME, null, 1);
DB_PATH = context.getFilesDir().getParent() + "/databases/";
this.myContext = context;
}
public void CreateDataBase()
{
boolean dbExists = checkDataBase();
if (!dbExists)
{
this.getReadableDatabase();
try
{
copyDataBase();
}
catch (Exception ex)
{
Log.w("hata","Veritabanı kopyalanamıyor");
throw new Error("Veritabanı kopyalanamıyor.");
}
}
}
boolean checkDataBase()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
catch (Exception ex)
{
Log.w("hata","Veritabanı açılamadı");
}
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
}
void copyDataBase()
{
try
{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myInput.close();
myOutput.close();
}
catch (Exception ex)
{
Log.w("hata", "Kopya oluşturma hatası.");
}
}
void openDataBase()
{
String myPath = DB_PATH + DB_NAME;
myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close()
{
if (myDatabase != null && myDatabase.isOpen())
myDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHelper dbHelper=new DatabaseHelper(this);
try
{
dbHelper.CreateDataBase();
}
catch (Exception ex)
{
Log.w("hata","Veritabanı oluşturulamadı ve kopyalanamadı!");
}
SQLiteDatabase db=dbHelper.getReadableDatabase();
String[] getColumnName={"student_name,student_surname"};
Cursor imlec=db.query("student", getColumnName, null, null, null, null, null);
ListView listview=(ListView)findViewById(R.id.listview);
ArrayList<String> student=new ArrayList<String>();
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,student);
while(imlec.moveToNext()){
String student_name=imlec.getString(imlec.getColumnIndex("student_name"));
String student_surname=imlec.getString(imlec.getColumnIndex("student_surname"));
String name_surname= student_name+ " "+student_surname;
student.add(name_surname);
}
listview.setAdapter(adapter);
imlec.close();
db.close();
}
}
140
Görüntülenme
0 Beğeni