我已经研究了这个并尝试更改我的数据库版本,以及重新启动应用程序,但到目前为止我没有运气。
06-04 13:58:54.803: E/AndroidRuntime(18361): FATAL EXCEPTION: main
06-04 13:58:54.803: E/AndroidRuntime(18361): android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, user, password FROM HELLO
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:691)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1436)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1283)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1154)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1322)
06-04 13:58:54.803: E/AndroidRuntime(18361): at yc.android.yourchallenger.DBAdapter.getAllContacts(DBAdapter.java:98)
06-04 13:58:54.803: E/AndroidRuntime(18361): at yc.android.yourchallenger.LoginActivity$1.onClick(LoginActivity.java:44)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.view.View.performClick(View.java:4489)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.view.View$PerformClick.run(View.java:18803)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.os.Handler.handleCallback(Handler.java:730)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.os.Looper.loop(Looper.java:137)
06-04 13:58:54.803: E/AndroidRuntime(18361): at android.app.ActivityThread.main(ActivityThread.java:5493)
06-04 13:58:54.803: E/AndroidRuntime(18361): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 13:58:54.803: E/AndroidRuntime(18361): at java.lang.reflect.Method.invoke(Method.java:525)
06-04 13:58:54.803: E/AndroidRuntime(18361): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-04 13:58:54.803: E/AndroidRuntime(18361): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-04 13:58:54.803: E/AndroidRuntime(18361): at dalvik.system.NativeStart.main(Native Method)
这是我的LoginActivity。当我按下登录按钮并将值传递到数据库时,它崩溃了。
package yc.android.yourchallenger;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ActionBar actionBar = getActionBar();
actionBar.hide();
final EditText mUserName = (EditText) findViewById(R.id.user_name);
final EditText mPassword = (EditText) findViewById(R.id.password);
final DBAdapter db = new DBAdapter(this);
final Button login = (Button) findViewById(R.id.login);
TextView signup = (TextView) findViewById(R.id.signup);
login.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
final String Username = mUserName.getText().toString();
final String Password= mPassword.getText().toString();
db.open();
Cursor c = db.getAllContacts();
while(c.moveToNext())
{
String c1=c.getString(2);
String c2=c.getString(3);
if(c1 == Username)
{
if(c2 == Password)
{
Toast.makeText(LoginActivity.this,
"You are succesfully logged in.",
Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(view.getContext(), Menu.class);
startActivityForResult(myIntent, 0);
}
else
{
Toast.makeText(LoginActivity.this, "Incorrect password",Toast.LENGTH_LONG).show();
}
Intent myIntent = new Intent(view.getContext(), LoginActivity.class);
startActivityForResult(myIntent, 0);
}
else
Toast.makeText(LoginActivity.this, "Incorrect",Toast.LENGTH_LONG).show();
}
db.close();
}
});
signup.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
Intent myIntent = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(myIntent);
}
});
}
}
这是我的数据库
package yc.android.yourchallenger;
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.SQLiteOpenHelper;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_USER = "user";
static final String KEY_PASSWORD = "password";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "HELLO";
static final String DATABASE_TABLE = "table";
static final long DATABASE_VERSION = 2;
static final String DATABASE_CREATE =
"create " +DATABASE_TABLE + " " + DATABASE_NAME +"(" +
KEY_USER + " TEXT, " +
KEY_PASSWORD + " TEXT);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter (Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(ctx);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, (int) DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
}
catch(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);
}
}
//opens db database
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return null;
}
//closes the database
public void close()
{
DBHelper.close();
}
//insert contacts
public long insertContact(String userName, String password )
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USER, userName);
initialValues.put(KEY_PASSWORD, password);
Toast.makeText(this.context, "contact inserted", Toast.LENGTH_SHORT).show();
return db.insert(DATABASE_NAME, null, initialValues);
}
//deletes contact
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_NAME, KEY_ROWID + "+" + rowId, null) > 0;
}
//retrieve contacts
public Cursor getAllContacts()
{
return db.query(DATABASE_NAME, new String[]{KEY_ROWID, KEY_USER,
KEY_PASSWORD}, null, null, null, null, null);
}
//retrieve specified contact
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_NAME, new String[] {KEY_ROWID,
KEY_USER, KEY_PASSWORD}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if(mCursor!= null){
mCursor.moveToFirst();
}
return mCursor;
}
public Boolean updatContact(long rowId, String user, String password)
{
ContentValues args = new ContentValues();
args.put(KEY_USER, user);
args.put(KEY_PASSWORD, password);
return db.update(DATABASE_NAME, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
答案 0 :(得分:2)
在创建表时,您忘记添加id列。 试着改变这个:
static final String DATABASE_CREATE =
"create " +DATABASE_TABLE + " " + DATABASE_NAME +"(" +
KEY_USER + " TEXT, " +
KEY_PASSWORD + " TEXT);";
到此:
static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + " ("
+ KEY_ROWID + " integer primary key autoincrement,"
+ KEY_USER + " text,"
+ KEY_USER+ " text)";
答案 1 :(得分:1)
在SQLite中,有一个默认的id列。它有几个别名,其中没有_id
。
可以使用一个与特殊情况无关的名称&#34; rowid&#34;,&#34; oid&#34;或&#34; _rowid _&#34;来访问rowid值。代替列名。
来自:http://www.sqlite.org/lang_createtable.html部分 ROWID和INTEGER PRIMARY KEY
但是,出于某种原因,android CursorAdapter
要求始终在所使用的游标中包含_id
列。您可以使用select rowid as _id, ...
答案 2 :(得分:0)
我认为问题在于您的DATABASE_CREATE命令。
我知道使用SQLLite创建数据库的方式就像这样
"CREATE TABLE " + DATABASE_TABLE + " ( "+ KEY_ROWID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+ KEY_USER +" VARCHAR(32), "+ KEY_PASSWORD +" VARCHAR(16) );"
我认为你的命令中有2个错误。