我试图将文本插入数据库。但是我没有成功创造这张桌子。我得到了Error Inserting:.... no such table
。
我的数据库类。
package com.msvd.form;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DataBase {
static final String KEY_ID = "_id";
static final String KEY_NAME= "name";
static final String DATABASE_NAME="MyDB";
static final String DATABASE_TABLE="contactlist";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE ="create table contactlist(_id integer primary key autoincrement,"
+"name text not null);";
Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DataBase(Context cxt){
this.context = cxt;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
public DataBase open(){
db = DBHelper.getWritableDatabase();
Toast.makeText(context, "Database open", Toast.LENGTH_SHORT).show();
return this;
}
public void close(){
DBHelper.close();
Toast.makeText(context, "Database close", Toast.LENGTH_SHORT).show();
}
public long insert(String name){
ContentValues value = new ContentValues();
value.put(KEY_NAME,name);
return db.insert(DATABASE_TABLE, null, value);
}
}
我在fragment_main.xml中创建了一个edittext字段,如下所示
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.msvd.form.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="222dp"
android:layout_marginTop="146dp"
android:text="@string/username"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/et_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="116dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="@+id/bt_Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_Name"
android:layout_marginLeft="55dp"
android:layout_marginTop="45dp"
android:layout_toRightOf="@+id/textView1"
android:onClick="onClick"
android:text="@string/save" />
</RelativeLayout>
我的mainactivity类包含,
package com.msvd.form;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
private EditText et_name;
private Button bt_save;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final Context thiscontainer = container.getContext();
et_name = (EditText) rootView.findViewById(R.id.et_Name);
bt_save = (Button) rootView.findViewById(R.id.bt_Save);
final DataBase db = new DataBase(thiscontainer);
bt_save.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
db.open();
db.insert(et_name.getText().toString());
db.close();
}
});
return rootView;
}
}
}
我收到的错误是:
04-13 20:54:28.872: E/SQLiteDatabase(5054): Error inserting name=aaaa
04-13 20:54:28.872: E/SQLiteDatabase(5054): android.database.sqlite.SQLiteException: no such table: contactlist: , while compiling: INSERT INTO contactlist(name) VALUES (?)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at com.msvd.form.DataBase.insert(DataBase.java:64)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at com.msvd.form.MainActivity$PlaceholderFragment$1.onClick(MainActivity.java:77)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.view.View.performClick(View.java:3511)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.view.View$PerformClick.run(View.java:14105)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.os.Handler.handleCallback(Handler.java:605)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.os.Looper.loop(Looper.java:137)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 20:54:28.872: E/SQLiteDatabase(5054): at dalvik.system.NativeStart.main(Native Method)
Manifest.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.msvd.form"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.msvd.form.LoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.msvd.form.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter >
<action android:name="com.msvd.form.MainActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
</manifest>
请建议我如何创建数据库表。
答案 0 :(得分:1)
问题在于创建表格。
static final String KEY_ID = "_id";
static final String KEY_NAME= "name";
static final String DATABASE_NAME="MyDB";
static final String DATABASE_TABLE="contactlist";
static final int DATABASE_VERSION = 1;
CHANGE FROM THIS
static final String DATABASE_CREATE ="create table contactlist(_id integer primary key autoincrement,"
+"name text not null);";
TO THIS
static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS "
+ DATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, "
+ KEY_NAME + " text not null);";