即時嘗試在數據庫中添加新表,但是每當我運行我的應用程序時,它給我一個錯誤tablename不存在,如何添加一個具有samoe字段和函數的表從用戶那裏獲取數據,並將其寫入活動的編輯文本中。如何在現有數據庫中添加新表
package com.example.smscampaign;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Comment;
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.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelp {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "person_name";
public static final String KEY_SCALE = "scale_person";
public static final String KEY_CONTACTS = "Contacts_person";
public static final String KEY_To_ROWID = "_id";
public static final String KEY_To_NAME = "person_name";
public static final String KEY_To_SCALE = "scale_person";
public static final String KEY_To_CONTACTS = "Contacts_person";
private static final String DATABASE_NAME = "Himani";
static final String DATABASE_TABLE = "peopleTable";
private static final String TABLE_ADD_MESSAGE = "Add_Message";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHepler;
private final Context ourContext;
SQLiteDatabase ourDatabase;
public class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_SCALE + " TEXT NOT NULL ," + KEY_CONTACTS + ")"
);
String CREATE_ADD_MESSAGE= "CREATE TABLE " + TABLE_ADD_MESSAGE +
"(" + KEY_To_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_To_NAME + " TEXT NOT NULL, " +
KEY_To_SCALE + " TEXT NOT NULL ," + KEY_To_CONTACTS + ")" ;
db.execSQL(CREATE_ADD_MESSAGE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
db.execSQL("DROP TABLE IF EXISTS" + TABLE_ADD_MESSAGE);
onCreate(db);
}
}
public DatabaseHelp(Context c) {
ourContext = c;
}
public DatabaseHelp open() throws SQLException {
ourHepler = new DbHelper(ourContext);
ourDatabase = ourHepler.getWritableDatabase();
return this;
}
public void close() {
ourHepler.close();
}
public long entryCreate(String name, String scale, String contacts) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_SCALE, scale);
cv.put(KEY_CONTACTS, contacts);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public long entryCreate1(String name, String scale, String contacts) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_To_NAME, name);
cv.put(KEY_To_SCALE, scale);
cv.put(KEY_To_CONTACTS, contacts);
return ourDatabase.insert(TABLE_ADD_MESSAGE, null, cv);
}
public ArrayList<String> getData() {
String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
KEY_CONTACTS };
Cursor c = ourDatabase.query(DATABASE_TABLE, col, null, null, null,
null, null);
String run = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iScale = c.getColumnIndex(KEY_SCALE);
int iMessage = c.getColumnIndex(KEY_CONTACTS);
ArrayList<String> newList = new ArrayList<String>();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
newList.add(c.getString(iName));
}
return newList;
}
public ArrayList<String> getData1() {
String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_To_SCALE,
KEY_To_CONTACTS };
Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, null, null, null,
null, null);
String run = "";
int iRow = c.getColumnIndex(KEY_To_ROWID);
int iName = c.getColumnIndex(KEY_To_NAME);
int iScale = c.getColumnIndex(KEY_To_SCALE);
int iMessage = c.getColumnIndex(KEY_To_CONTACTS);
ArrayList<String> newList = new ArrayList<String>();
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
newList.add(c.getString(iName));
}
return newList;
}
public Cursor fetchChildren(String KEY_) {
Cursor c = ourDatabase.rawQuery("SELECT * FROM " + DATABASE_TABLE
+ " WHERE person_name = ?", new String[] { KEY_ });
return c;
}
public Cursor fetchChildren1(String KEY_) {
Cursor c = ourDatabase.rawQuery("SELECT * FROM " + DATABASE_TABLE
+ " WHERE person_name = ?", new String[] { KEY_ });
return c;
}
public String getScale(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
KEY_CONTACTS };
Cursor c = ourDatabase.query(DATABASE_TABLE, col, KEY_ROWID + "-" + l,
null, null, null, null);
if (c != null) {
c.moveToFirst();
String scale = c.getString(2);
return scale;
}
return null;
}
public String getScale1(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_To_SCALE,
KEY_To_CONTACTS };
Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, KEY_To_ROWID + "-"
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String scale = c.getString(2);
return scale;
}
return null;
}
public String getName(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
KEY_CONTACTS };
Cursor c = ourDatabase.query(DATABASE_TABLE, col, KEY_ROWID + "-" + l,
null, null, null, null);
if (c != null) {
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getName1(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_SCALE,
KEY_To_CONTACTS };
Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, KEY_To_ROWID + "-"
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public String getContacts(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
KEY_CONTACTS };
Cursor c = ourDatabase.query(DATABASE_TABLE, col, KEY_ROWID + "-" + l,
null, null, null, null);
if (c != null) {
c.moveToFirst();
String contacts = c.getString(3);
return contacts;
}
return null;
}
public String getContacts1(long l) {
// TODO Auto-generated method stub
String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_To_SCALE,
KEY_To_CONTACTS };
Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, KEY_To_ROWID + "-"
+ l, null, null, null, null);
if (c != null) {
c.moveToFirst();
String contacts = c.getString(3);
return contacts;
}
return null;
}
public void updateEntry(long lt, String mName, String mScale,
String mContatcs) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME, mName);
cvUpdate.put(KEY_SCALE, mScale);
cvUpdate.put(KEY_CONTACTS, mContatcs);
ourDatabase
.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "-" + lt, null);
}
public void updateEntry1(long lt, String mName, String mScale,
String mContatcs) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_To_NAME, mName);
cvUpdate.put(KEY_To_SCALE, mScale);
cvUpdate.put(KEY_To_CONTACTS, mContatcs);
ourDatabase.update(TABLE_ADD_MESSAGE, cvUpdate,
KEY_To_ROWID + "-" + lt, null);
}
public void deleteEntry1(long ltt) throws SQLException {
// TODO Auto-generated method stub
ourDatabase.delete(TABLE_ADD_MESSAGE, KEY_To_ROWID + "=" + ltt, null);
}
public void deleteEntry(long ltt) throws SQLException {
// TODO Auto-generated method stub
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + ltt, null);
}
}
總是得到這個錯誤
> 03-19 16:46:52.396: E/AndroidRuntime(10089):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.smscampaign/com.example.smscampaign.Campaign_Details}:
> android.database.sqlite.SQLiteException: no such table: Add_Message
> (code 1): , while compiling: SELECT _id, name, message, Contacts FROM
> Add_Message
檢查此問題http://stackoverflow.com/questions/22502453/android-what-is-the-easist-way-upgrade-database-for-existing-db/22502748#22502748 –