SQLite数据库使用我的代码创建问题

时间:2015-04-19 22:42:35

标签: android sqlite

第一次使用SQLite时,我一直在使用在线教程向我展示如何创建数据库和表格。我的表应该有3列TEXT类型,分别是名称,电子邮件和密码。 不幸的是,它似乎不起作用,我真的不知道为什么,我认为这是由于这个错误

Caused by: android.database.sqlite.SQLiteException: near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE Registered_Info(UserName TEXT,Password TEXT,Email TEXT);

这是我的代码。

package com.example.wolfe_000.final_final_zeno;

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;

/**
 * Created by wolfe_000 on 19/04/2015.
 */
public class DatabaseOperations extends SQLiteOpenHelper {

public static int database_version = 1;
public String CREATE_QUERY = "CREATE_TABLE "+ TableData.TableInfo.TABLE_NAME+"("+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_PASS+" TEXT,"+TableData.TableInfo.USER_EMAIL+" TEXT);";



public DatabaseOperations(Context context){
    super(context, TableData.TableInfo.DATABASE_NAME,null,database_version);
    Log.d("Database Operations", "Database Created");
}

@Override
public void onCreate(SQLiteDatabase sdb){
    sdb.execSQL(CREATE_QUERY);
    Log.d("Database Operations", "Database Created");
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

}

public void putInformation(DatabaseOperations dop, String name, String pass, String email){
    SQLiteDatabase SQ = dop.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(TableData.TableInfo.USER_NAME, name);
    cv.put(TableData.TableInfo.USER_PASS, pass);
    cv.put(TableData.TableInfo.USER_EMAIL, email);
    long k = SQ.insert(TableData.TableInfo.TABLE_NAME,null, cv);
    Log.d("Database Operations", "One Raw Inserted");
}

}

使用此代码单击我的SignUp按钮时发生错误:

public void SignUpOnClick(View view ){
    user_name = USER_NAME.getText().toString();
    user_email = USER_EMAIL.getText().toString();
    user_pass = USER_PASS.getText().toString();

    DatabaseOperations DB = new DatabaseOperations(ctx);
    DB.putInformation(DB, user_name, user_email, user_pass);
    Toast.makeText(getBaseContext(), "Successfully Registered", Toast.LENGTH_LONG).show();
    finish();
}

2 个答案:

答案 0 :(得分:2)

CREATE TABLE”不是“CREATE_TABLE”!!!!!

答案 1 :(得分:1)

问题出在这里:CREATE TABLE而不是CREATE_TABLE

public String CREATE_QUERY = "CREATE TABLE "+ TableData.TableInfo.TABLE_NAME+"("+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_PASS+" TEXT,"+TableData.TableInfo.USER_EMAIL+" TEXT);";