我尝试使用SQLite在Android中创建数据库。我有一个单独的类将值插入数据库,但我收到的错误表明数据库无法创建,我不知道为什么!任何帮助将不胜感激。
package com.example.siuni.mymedicare;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.sql.SQLException;
/**
* Created by SiUni on 23/04/2015.
*/
public class MyDBHandler {
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FIRSTNAME = "_firstname";
public static final String COLUMN_SURNAME = "_surname";
public static final String COLUMN_TELEPHONENO = "_telephoneno";
public static final String COLUMN_POSTCODE = "_postcode";
private static final String TAG = "MyDBHelper";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mymedicare.db";
private static final String TABLE_DATABASEHANDLER = "Databasehandler";
private static final String DATABASE_CREATE =
"create table contacts (_id integer primary key autoincrement, " +
"name text not null, surname not null, telephoneno notnull, postcode not null);";
public final Context context;
private DatabaseHandler DBHandler;
public SQLiteDatabase db;
public MyDBHandler(Context ctx) {
this.context = ctx;
DBHandler = new DatabaseHandler(context);
}
private static class DatabaseHandler extends SQLiteOpenHelper {
DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (android.database.SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
public MyDBHandler open() throws SQLException {
db = DBHandler.getWritableDatabase();
return this;
}
public void close() {
DBHandler.close();
}
public long insertContact(String Firstname, String Surname, String Telephone,
String Postcode) {
ContentValues insertValues = new ContentValues();
insertValues.put(COLUMN_FIRSTNAME, Firstname);
insertValues.put(COLUMN_SURNAME, Surname);
insertValues.put(COLUMN_TELEPHONENO, Telephone);
insertValues.put(COLUMN_POSTCODE, Postcode);
return db.insert(TABLE_DATABASEHANDLER, null, insertValues);
}
public Cursor getAllContacts ()
{
return db.query(TABLE_DATABASEHANDLER, new String [] {COLUMN_ID, COLUMN_FIRSTNAME,
COLUMN_SURNAME}, null, null, null, null, null);
}
public Cursor getContact (long rowId) throws android.database.SQLException
{
Cursor mCursor =
db.query(true, TABLE_DATABASEHANDLER, new String[] {COLUMN_ID, COLUMN_FIRSTNAME,
COLUMN_SURNAME}, COLUMN_ID + "=" + rowId, null, null,null,null,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
错误日志:
05-12 16:50:07.348 2466-2466/com.example.siuni.mymedicare E/SQLiteDatabase﹕ Error inserting _surname= _postcode= _telephoneno= _firstname=
android.database.sqlite.SQLiteException: no such table: Databasehandler (code 1): , while compiling: INSERT INTO Databasehandler(_surname,_postcode,_telephoneno,_firstname) VALUES (?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.siuni.mymedicare.MyDBHandler.insertContact(MyDBHandler.java:85)
at com.example.siuni.mymedicare.register.onCreate(register.java:45)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
提前感谢您的帮助
答案 0 :(得分:0)
问题在于:
byte[] byteArray = File.ReadAllBytes("C:\\temp\\sa123.potx");
using (MemoryStream stream = new MemoryStream())
{
stream.Write(byteArray, 0, (int)byteArray.Length);
using (PresentationDocument presentationDoc = PresentationDocument.Open(stream, true))
{
// Change from template type to presentation type
presentationDoc.ChangeDocumentType(PresentationDocumentType.Presentation);
}
File.WriteAllBytes("C:\\temp\\sa123.pptx", stream.ToArray());
}
在您的代码中,您使用private static final String TABLE_DATABASEHANDLER = "**Databasehandler**";
作为表名,但您没有名为TABLE_DATABASEHANDLER
的表。
从数据库创建语句中可以看出,您只有一个表 - Databasehandler
:
contacts
答案 1 :(得分:0)
您正在尝试将值插入Databasehandler
表,并且在您的创建SQL中只有一个表,称为contacts
。 create table contacts (_id integer pr....
只需更改String TABLE_DATABASEHANDLER = "Databasehandler";
变量的值