- Forum-Beiträge: 16
25.01.2013, 20:24:52 via Website
25.01.2013 20:24:52 via Website
ich bin neue hier im Forum^^
und ich habe ein mittelschweres Problem.
Ich bin jetzt auch so langsam in die Android-Programmierung eingestiegen und bastel auch schon an meiner ersten App (mit eclipse) . Diese soll eine Datenbank enthalten. Ich habe es auch hingekriegt. Die Datenbank funktioniert und eine Speicherfunktion ist auch vorhanden. Die Speicherfunktion soll aber nur als Ergänzung dienen, dass also noch eigene Sachen zur Datenbank hinzufügt werden können.
Ich bin zuerst nach diesem Tutorial auf "vogella.com/articles/AndroidSQLite/article.html" gegangen um die DB zu erzeugen. Danach wollte ich die DB mit einigen Werten füllen (zum Test habe ich erstmal 3 genommen). Dazu habe ich diese Seite verwendet "reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/".
Ging alles soweit ganz gut. Bis auf einen Fehler. Der zweite Quelltextabschnitt aus dem 2. Link ist das Problem. Beim "DataBaseHelper myDbHelper = new DataBaseHelper();" bekomme ich den Error:"DataBaseHelper cannot be resolved to a type." Ich hab das ja bei mir etwas umbenannt, um das an meine App anzupassen, aber er kreidet mir immer noch bei "CocktailDatabaseHelper myDbHelper = new DataBaseHelper();" das zweite "DataBaseHelper" an und der ERROR bleibt:"DataBaseHelper cannot be resolved to a type." Ich habe mich durch x Internetseiten gelesen und weiß einfach nicht mehr weiter.
Ich hoffe daher, dass ihr mir weiterhelfen könnt. Danke schon mal im Voraus
PS: Sorry für die etwas längere Beschreibung ^^
Hier ist mein Quellcode:
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.OutputStream;
7
8import de.android.app.cocktailbar.R;
9
10import android.content.Context;
11import android.database.SQLException;
12import android.database.sqlite.SQLiteDatabase;
13import android.database.sqlite.SQLiteException;
14import android.database.sqlite.SQLiteOpenHelper;
15
16
17public class CocktailDatabaseHelper extends SQLiteOpenHelper {
18
19 //The Android's default system path of your application database.
20 private static String DB_PATH = "/data/data/de.android.app.cocktailbar.database/databases/";
21
22 private static String TABLE_COCKTAIL = "cocktail";
23
24 private SQLiteDatabase cocktailDB;
25
26 private final Context CocktailContext;
27
28 /**
29 * Constructor
30 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
31 * @param context
32 */
33 public CocktailDatabaseHelper(Context context) {
34
35 super(context, TABLE_COCKTAIL, null, 1);
36 this.CocktailContext = context;
37 }
38
39 /**
40 * Creates a empty database on the system and rewrites it with your own database.
41 * */
42 public void createDataBase() throws IOException{
43
44 boolean dbExist = checkDataBase();
45
46 if(dbExist){
47 //do nothing - database already exist
48 }else{
49
50 //By calling this method and empty database will be created into the default system path
51 //of your application so we are gonna be able to overwrite that database with our database.
52 this.getReadableDatabase();
53
54 try {
55
56 copyDataBase();
57
58 } catch (IOException e) {
59
60 throw new Error("Error copying database");
61
62 }
63 }
64
65 }
66
67 /**
68 * Check if the database already exist to avoid re-copying the file each time you open the application.
69 * @return true if it exists, false if it doesn't
70 */
71 private boolean checkDataBase(){
72
73 SQLiteDatabase checkDB = null;
74
75 try{
76 String myPath = DB_PATH + TABLE_COCKTAIL;
77 checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
78
79 }catch(SQLiteException e){
80
81 //database does't exist yet.
82
83 }
84
85 if(checkDB != null){
86
87 checkDB.close();
88
89 }
90
91 return checkDB != null ? true : false;
92 }
93
94 /**
95 * Copies your database from your local assets-folder to the just created empty database in the
96 * system folder, from where it can be accessed and handled.
97 * This is done by transfering bytestream.
98 * */
99 private void copyDataBase() throws IOException{
100
101 //Open your local db as the input stream
102 InputStream myInput = CocktailContext.getAssets().open(TABLE_COCKTAIL);
103
104 // Path to the just created empty db
105 String outFileName = DB_PATH + TABLE_COCKTAIL;
106
107 //Open the empty db as the output stream
108 OutputStream myOutput = new FileOutputStream(outFileName);
109
110 //transfer bytes from the inputfile to the outputfile
111 byte[] buffer = new byte[1024];
112 int length;
113 while ((length = myInput.read(buffer))>0){
114 myOutput.write(buffer, 0, length);
115 }
116
117 //Close the streams
118 myOutput.flush();
119 myOutput.close();
120 myInput.close();
121
122 }
123
124 public void openDataBase() throws SQLException{
125
126 //Open the database
127 String myPath = DB_PATH + TABLE_COCKTAIL;
128 cocktailDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
129
130 }
131
132 @Override
133 public synchronized void close() {
134
135 if(cocktailDB != null)
136 cocktailDB.close();
137
138 super.close();
139
140 }
141
142 @Override
143 public void onCreate(SQLiteDatabase db) {
144
145 }
146
147 @Override
148 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
149
150 }
151
152 CocktailDatabaseHelper myDbHelper = new DataBaseHelper();
153 myDbHelper = new DataBaseHelper(this);
154
155 try {
156
157 myDbHelper.createDataBase();
158
159 } catch (IOException ioe) {
160
161 throw new Error("Unable to create database");
162
163 }
164
165 try {
166
167 myDbHelper.openDataBase();
168
169 }catch(SQLException sqle){
170
171 throw sqle;
172
173 }
174
175 }
176}
— geändert am 26.01.2013, 11:44:56
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.