我正在创建SqLite应用程序...应用程序完美启动但是当我填写所有EditText字段并单击添加按钮时...应用程序已停止..此消息弹出...
MainActivity.java
package com.example.database;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
EditText first,last,age,classc;
Button add,view;
String FirstName;
String LastName;
String Class;
String Age;
sqLit myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB = new sqLit(this);
Link();
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
xmlToVar();
Integer flag=myDB.insertValue(FirstName, LastName, Class, Age);
if (flag==1)
{
Context context=MainActivity.this;
Toast toast = Toast.makeText(context, "Record Added" , Toast.LENGTH_LONG);
toast.show();
}
else
{
Context context=MainActivity.this;
Toast toast = Toast.makeText(context, "Error Occured" , Toast.LENGTH_LONG);
toast.show();
}
}
});
}
public void Link()
{
first=(EditText) findViewById(R.id.editFirst);
last=(EditText) findViewById(R.id.editLast);
classc=(EditText) findViewById(R.id.editClass);
age=(EditText) findViewById(R.id.editAge);
add=(Button) findViewById(R.id.Add);
view=(Button) findViewById(R.id.ViewAll);
}
public void xmlToVar()
{
FirstName = first.getText().toString();
LastName = last.getText().toString();
Class = classc.getText().toString();
try
{
Age = age.getText().toString();
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
}
}
sqLit.java
package com.example.database;
import org.w3c.dom.Text;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.text.Editable;
public class sqLit extends SQLiteOpenHelper
{
public static final String DB_NAME="student12.db";
public static final String TABLE_NAME="class1";
public static final String COL_1="ROLLNO";
public static final String COL_2="FirstName";
public static final String COL_3="LastName";
public static final String COL_4="Class";
public static final String COL_5="Age";
public sqLit(Context context) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + "(" +COL_1 + " INTEGER AUTOINCREMENT PRIMARY KEY,"+COL_2 + " TEXT,"+COL_3 + " TEXT,"+COL_4 + " TEXT,"+COL_5 + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("Drop Table If Exist" + TABLE_NAME );
onCreate(db);
}
public Integer insertValue(String firstName, String lastName, String Class, String Age)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentV = new ContentValues();
contentV.put(COL_2, firstName);
contentV.put(COL_3, lastName);
contentV.put(COL_4, Class);
contentV.put(COL_5, Age);
long isInserted = db.insert(TABLE_NAME, null, contentV);
if (isInserted == -1){
return 0;
}
else
{
return 1;
}
}
}
logcat的
06-11 20:28:40.925: W/dalvikvm(25232): threadid=1: thread exiting with uncaught exception (group=0xb3f2b288)
06-11 20:28:40.925: E/AndroidRuntime(25232): FATAL EXCEPTION: main
06-11 20:28:40.925: E/AndroidRuntime(25232): android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE class1(ROLLNO INTEGER AUTOINCREMENT PRIMARY KEY,FirstName TEXT,LastName TEXT,Class TEXT,Age TEXT)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
06-11 20:28:40.925: E/AndroidRuntime(25232): at com.example.database.sqLit.onCreate(sqLit.java:35)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
06-11 20:28:40.925: E/AndroidRuntime(25232): at com.example.database.sqLit.insertValue(sqLit.java:48)
06-11 20:28:40.925: E/AndroidRuntime(25232): at com.example.database.MainActivity$1.onClick(MainActivity.java:44)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.view.View.performClick(View.java:4084)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.view.View$PerformClick.run(View.java:16966)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.os.Handler.handleCallback(Handler.java:615)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.os.Looper.loop(Looper.java:137)
06-11 20:28:40.925: E/AndroidRuntime(25232): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-11 20:28:40.925: E/AndroidRuntime(25232): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 20:28:40.925: E/AndroidRuntime(25232): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 20:28:40.925: E/AndroidRuntime(25232): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-11 20:28:40.925: E/AndroidRuntime(25232): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-11 20:28:40.925: E/AndroidRuntime(25232): at dalvik.system.NativeStart.main(Native Method)
06-11 20:28:40.944: D/dalvikvm(25232): GC_CONCURRENT freed 203K, 5% free 6173K/6471K, paused 13ms+0ms, total 16ms
谢谢大家......:)
答案 0 :(得分:2)
您可以尝试重新调整SQL自动增量位置吗?
db.execSQL("CREATE TABLE " + TABLE_NAME + "(" +COL_1 + " INTEGER PRIMARY KEY AUTOINCREMENT,"+COL_2 + " TEXT,"+COL_3 + " TEXT,"+COL_4 + " TEXT,"+COL_5 + " TEXT)");
编辑:
实际上,我可能已经过度复杂了。它出现(根据{Lite {3}}上的SQL Lite文档):
&#34;如果您将表的列声明为INTEGER PRIMARY KEY,那么 无论何时向表的该列插入NULL,NULL都是 自动转换为一个大于的整数 该列中所有其他行的最大值,如果为1,则为1 表格是空的。&#34;
所以只要你稍后在那里插入空值,你就可以完全省略AUTOINCREMENT。