在我的 Android 程序中,我需要1 DB 中的 3 个 DB或3 个 DB 表。首先会有一些数据。二是要有密码。我正在创建第二个。第一个编译没有问题,但第二个给了我一些错误,在过去的 10 个小时里,我无法修复它。(我克隆了第一个数据库表,粘贴到第二个并更改了变量、名称等。我控制了几次,没有语法错误?) 注意:我是开发正确登录过程的新手。这是我的作业,教授不希望我给他一个正确的登录过程,所以我不需要安全性等。密码是否正确。登录是否正常。
一些帮助会很棒,正如我所说,我浪费了我的 10 个小时......
数据库适配器
package com.example.tsfc;
import java.io.IOException;
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;
public class DBLogin {
public static final String KEY_SELECT = "select";
private static final String TAG = "DBLogin";
private static final String DATABASE_NAME = "DBLog";
private static final String DATABASE_TABLE = "log";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_CREATE = "create table if not exists log (" +
"select string not null " +
");";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBLogin(Context ctx){
this.context = ctx;
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){
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);
}
}
public DBLogin open() throws SQLException{
db = DBHelper.getWritableDatabase();
return this;
}
public void close(){
DBHelper.close();
}
public long insertRecord(String select){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_SELECT, select);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public Cursor getAllRecords()
{
return db.query(false, DATABASE_TABLE, new String[] {KEY_SELECT}, null, null, null, null, null, null);
}
public Cursor getRecord(long rowId) throws IOException {
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_SELECT},
null, null, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateRecord(long rowID, String select){
ContentValues args = new ContentValues();
args.put(KEY_SELECT, select);
return db.update(DATABASE_TABLE, args, null, null) > 0;
}
public String[] displayRecord(Cursor cr)
{
String array[] = {cr.getString(0), cr.getString(1), cr.getString(2), cr.getString(3), cr.getString(4), cr.getString(5)};
return array;
}
}
在 main.java 中创建数据库
package com.example.tsfc;
//some imports.....
public class MainActivity extends Activity {
static{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button userButton = (Button)findViewById(R.id.user_button);
userButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, ShowMessagesPage.class);
startActivity(i);
}
});
try {
String destPath = "/data/data/" + getPackageName() + "/databases/DBMessages";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("meee"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Button loginButton =(Button)findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, LoginPage.class);
startActivity(i);
}
});
try {
String destPath = "/data/data/" + getPackageName() + "/databases/DBLog";
File f = new File(destPath);
if (!f.exists()) {
CopyDB( getBaseContext().getAssets().open("meeemeeee"),
new FileOutputStream(destPath));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
boolean checkConnection(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
}
else {
return false;
}
}
public void sendMessage(View view) {
Intent intent = new Intent(this, ShowMessagesPage.class);
if (checkConnection()){
startActivity(intent);
}
else {Toast.makeText(this, "There is no internet connection. Please check and try again.", Toast.LENGTH_SHORT).show();
return;
}
}
public void login(View view) {
Intent intent = new Intent(this, LoginPage.class);
if (checkConnection()){
startActivity(intent);
}
else {Toast.makeText(this, "There is no internet connection. Please check and try again.", Toast.LENGTH_SHORT).show();
return;
}
}}
在数据库上工作
public class LoginPage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
Button loginButton = (Button)findViewById(R.id.login);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
isOK();
} catch (IOException e) {
e.printStackTrace();
}
}});}
public String sman;
public String sdev;
public String spass;
public String sor;
boolean checkConnection(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
}
else {
return false;
}
}
private void isOK() throws IOException{
DBLogin db = new DBLogin(this);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
EditText passEditText = (EditText) findViewById(R.id.pass);
spass = passEditText.getText().toString();
String sor = spinner.getSelectedItem().toString();
db.open();
long id = db.insertRecord("asdasd123");
id = db.insertRecord("qweqwe123");
db.close();
if (spass.matches("")) {
Toast.makeText(this, "You have entered something wrong. Please check.", Toast.LENGTH_SHORT).show();
return;}
else {
if(checkConnection()){
Log.d("test", "adding");
db.open();
Cursor cr = db.getRecord(1);
Cursor cr2 = db.getRecord(2);
if(spass == cr.toString() && sor == "Manager") {
Toast.makeText(this,"Password confirmed", Toast.LENGTH_LONG).show();
Intent i = new Intent(this, ManagerView.class);
startActivity(i);}
else if (spass == cr2.toString() && sor == "Developer"){
Toast.makeText(this,"Password confirmed", Toast.LENGTH_LONG).show();
Intent a = new Intent(this, DeveloperView.class);
startActivity(a);}
else {
Toast.makeText(this,"Password is incorrect", Toast.LENGTH_LONG).show();
return;
}
db.close();
return;}
else {Toast.makeText(this, "There is no internet connection. Please check and try again.", Toast.LENGTH_SHORT).show();
return;
}
}}}
LOGCAT
12-30 10:53:45.871: E/SQLiteLog(907): (1) near "select": syntax error
12-30 10:53:45.901: E/SQLiteDatabase(907): Error inserting select=asdasd123
12-30 10:53:45.901: E/SQLiteDatabase(907): android.database.sqlite.SQLiteException: near "select": syntax error (code 1): , while compiling: INSERT INTO log(select) VALUES (?)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
12-30 10:53:45.901: E/SQLiteDatabase(907): at com.example.tsfc.DBLogin.insertRecord(DBLogin.java:82)
12-30 10:53:45.901: E/SQLiteDatabase(907): at com.example.tsfc.LoginPage.error(LoginPage.java:77)
12-30 10:53:45.901: E/SQLiteDatabase(907): at com.example.tsfc.LoginPage.access$0(LoginPage.java:63)
12-30 10:53:45.901: E/SQLiteDatabase(907): at com.example.tsfc.LoginPage$1.onClick(LoginPage.java:31)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.view.View.performClick(View.java:4202)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.view.View$PerformClick.run(View.java:17340)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.os.Handler.handleCallback(Handler.java:725)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.os.Looper.loop(Looper.java:137)
12-30 10:53:45.901: E/SQLiteDatabase(907): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-30 10:53:45.901: E/SQLiteDatabase(907): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 10:53:45.901: E/SQLiteDatabase(907): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 10:53:45.901: E/SQLiteDatabase(907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-30 10:53:45.901: E/SQLiteDatabase(907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-30 10:53:45.901: E/SQLiteDatabase(907): at dalvik.system.NativeStart.main(Native Method)
12-30 10:53:45.901: E/SQLiteLog(907): (1) near "select": syntax error
12-30 10:53:45.950: E/SQLiteDatabase(907): Error inserting select=qweqwe123
12-30 10:53:45.950: E/SQLiteDatabase(907): android.database.sqlite.SQLiteException: near "select": syntax error (code 1): , while compiling: INSERT INTO log(select) VALUES (?)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
12-30 10:53:45.950: E/SQLiteDatabase(907): at com.example.tsfc.DBLogin.insertRecord(DBLogin.java:82)
12-30 10:53:45.950: E/SQLiteDatabase(907): at com.example.tsfc.LoginPage.error(LoginPage.java:78)
12-30 10:53:45.950: E/SQLiteDatabase(907): at com.example.tsfc.LoginPage.access$0(LoginPage.java:63)
12-30 10:53:45.950: E/SQLiteDatabase(907): at com.example.tsfc.LoginPage$1.onClick(LoginPage.java:31)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.view.View.performClick(View.java:4202)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.view.View$PerformClick.run(View.java:17340)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.os.Handler.handleCallback(Handler.java:725)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.os.Looper.loop(Looper.java:137)
12-30 10:53:45.950: E/SQLiteDatabase(907): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-30 10:53:45.950: E/SQLiteDatabase(907): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 10:53:45.950: E/SQLiteDatabase(907): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 10:53:45.950: E/SQLiteDatabase(907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-30 10:53:45.950: E/SQLiteDatabase(907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-30 10:53:45.950: E/SQLiteDatabase(907): at dalvik.system.NativeStart.main(Native Method)
12-30 10:53:45.960: D/test(907): adding
12-30 10:53:45.990: E/SQLiteLog(907): (1) near "select": syntax error
12-30 10:53:45.990: D/AndroidRuntime(907): Shutting down VM
12-30 10:53:45.990: W/dalvikvm(907): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-30 10:53:46.010: E/AndroidRuntime(907): FATAL EXCEPTION: main
12-30 10:53:46.010: E/AndroidRuntime(907): android.database.sqlite.SQLiteException: near "select": syntax error (code 1): , while compiling: SELECT DISTINCT select FROM log
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1073)
12-30 10:53:46.010: E/AndroidRuntime(907): at com.example.tsfc.DBLogin.getRecord(DBLogin.java:95)
12-30 10:53:46.010: E/AndroidRuntime(907): at com.example.tsfc.LoginPage.error(LoginPage.java:89)
12-30 10:53:46.010: E/AndroidRuntime(907): at com.example.tsfc.LoginPage.access$0(LoginPage.java:63)
12-30 10:53:46.010: E/AndroidRuntime(907): at com.example.tsfc.LoginPage$1.onClick(LoginPage.java:31)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.view.View.performClick(View.java:4202)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.view.View$PerformClick.run(View.java:17340)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.os.Handler.handleCallback(Handler.java:725)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.os.Handler.dispatchMessage(Handler.java:92)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.os.Looper.loop(Looper.java:137)
12-30 10:53:46.010: E/AndroidRuntime(907): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-30 10:53:46.010: E/AndroidRuntime(907): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 10:53:46.010: E/AndroidRuntime(907): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 10:53:46.010: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-30 10:53:46.010: E/AndroidRuntime(907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-30 10:53:46.010: E/AndroidRuntime(907): at dalvik.system.NativeStart.main(Native Method)