Da Vid
- Forum-Beiträge: 11
17.07.2012, 16:40:26 via Website
17.07.2012 16:40:26 via Website
Hi,
ich habe nach dem Versuch eine Datenbank zu erstellen folgenden Fehler erhalten:
Ich finde den Fehler einfach nicht! Mein Hirn ist mittlerweile Brei ;) Hat jemand von euch was gesehen?
Brauche ich dafür spezielle Berechtigungen? Die Tabelle in der Datenbank wird doch mit dem SQL Befehl CREATE TABLE erstellt, ich muss also vorher nicht erstellt haben, oder?
Das ist meine DBHelper Klasse:
und das meine Zugriffmethode aus einer anderen Activity heraus:
ich habe nach dem Versuch eine Datenbank zu erstellen folgenden Fehler erhalten:
107-17 15:10:58.041: E/SQLiteLog(29452): (1) table peopleTable has no column named phone_number
Ich finde den Fehler einfach nicht! Mein Hirn ist mittlerweile Brei ;) Hat jemand von euch was gesehen?
Brauche ich dafür spezielle Berechtigungen? Die Tabelle in der Datenbank wird doch mit dem SQL Befehl CREATE TABLE erstellt, ich muss also vorher nicht erstellt haben, oder?
Das ist meine DBHelper Klasse:
1public class DBHelperMain {
2
3 public static final String KEY_ROWID = "_id";
4 public static final String KEY_NAME = "person_name";
5 public static final String KEY_PHONENUMBER = "phone_number";
6
7 private static final String DATABASE_NAME = "Contacts";
8 private static final String DATABASE_TABLE = "peopleTable";
9 private static final int DATABASE_VERSION = 1;
10
11 private DbHelper ourHelper;
12 private final Context ourContext;
13 private SQLiteDatabase ourDatabase;
14
15 private static class DbHelper extends SQLiteOpenHelper {
16
17 public DbHelper(Context context) {
18 super(context, DATABASE_NAME, null, DATABASE_VERSION);
19
20 }
21
22 @Override
23 public void onCreate(SQLiteDatabase db) {
24
25 db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
26 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
27 + KEY_NAME + " TEXT NOT NULL, "
28 + KEY_PHONENUMBER + " TEXT NOT NULL);"
29
30 );
31
32 }
33
34 @Override
35 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
36
37 db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
38 onCreate(db);
39
40 }
41
42 }
43
44 public DBHelperMain(Context c) {
45 ourContext = c;
46 }
47
48 public DBHelperMain open() throws SQLException {
49
50 ourHelper = new DbHelper(ourContext);
51 ourDatabase = ourHelper.getWritableDatabase();
52 return this;
53
54 }
55
56 public void close() {
57
58 ourHelper.close();
59 }
60
61 public long createEntry(String name, String phone_number) {
62
63 ContentValues cv = new ContentValues();
64 cv.put(KEY_NAME, name);
65 cv.put(KEY_PHONENUMBER, phone_number);
66 return ourDatabase.insert(DATABASE_TABLE, null, cv);
67
68 }
69
70}
2
3 public static final String KEY_ROWID = "_id";
4 public static final String KEY_NAME = "person_name";
5 public static final String KEY_PHONENUMBER = "phone_number";
6
7 private static final String DATABASE_NAME = "Contacts";
8 private static final String DATABASE_TABLE = "peopleTable";
9 private static final int DATABASE_VERSION = 1;
10
11 private DbHelper ourHelper;
12 private final Context ourContext;
13 private SQLiteDatabase ourDatabase;
14
15 private static class DbHelper extends SQLiteOpenHelper {
16
17 public DbHelper(Context context) {
18 super(context, DATABASE_NAME, null, DATABASE_VERSION);
19
20 }
21
22 @Override
23 public void onCreate(SQLiteDatabase db) {
24
25 db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
26 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
27 + KEY_NAME + " TEXT NOT NULL, "
28 + KEY_PHONENUMBER + " TEXT NOT NULL);"
29
30 );
31
32 }
33
34 @Override
35 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
36
37 db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
38 onCreate(db);
39
40 }
41
42 }
43
44 public DBHelperMain(Context c) {
45 ourContext = c;
46 }
47
48 public DBHelperMain open() throws SQLException {
49
50 ourHelper = new DbHelper(ourContext);
51 ourDatabase = ourHelper.getWritableDatabase();
52 return this;
53
54 }
55
56 public void close() {
57
58 ourHelper.close();
59 }
60
61 public long createEntry(String name, String phone_number) {
62
63 ContentValues cv = new ContentValues();
64 cv.put(KEY_NAME, name);
65 cv.put(KEY_PHONENUMBER, phone_number);
66 return ourDatabase.insert(DATABASE_TABLE, null, cv);
67
68 }
69
70}
und das meine Zugriffmethode aus einer anderen Activity heraus:
1public void onClick(View arg0) {
2
3 switch (arg0.getId()) {
4
5 case R.id.button2:
6
7 boolean didItWork = true;
8 try {
9 String name = autotext.getText().toString();
10 String phone_number = edittext3.getText().toString();
11
12 DBHelperMain entry = new DBHelperMain(this);
13 entry.open();
14 entry.createEntry(name, phone_number);
15 entry.close();
16
17 } catch (Exception e) {
18 didItWork = false;
19 } finally {
20 if (didItWork) {
21
22 Dialog d = new Dialog(this);
23 d.setTitle("YES");
24 TextView tv = new TextView(this);
25 tv.setText("I DID IT");
26 d.setContentView(tv);
27 d.show();
28
29 }
30
31 }
32
33 break;
34
35 }
2
3 switch (arg0.getId()) {
4
5 case R.id.button2:
6
7 boolean didItWork = true;
8 try {
9 String name = autotext.getText().toString();
10 String phone_number = edittext3.getText().toString();
11
12 DBHelperMain entry = new DBHelperMain(this);
13 entry.open();
14 entry.createEntry(name, phone_number);
15 entry.close();
16
17 } catch (Exception e) {
18 didItWork = false;
19 } finally {
20 if (didItWork) {
21
22 Dialog d = new Dialog(this);
23 d.setTitle("YES");
24 TextView tv = new TextView(this);
25 tv.setText("I DID IT");
26 d.setContentView(tv);
27 d.show();
28
29 }
30
31 }
32
33 break;
34
35 }
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.