Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我为本地sqlite数据库创建了应用程序,但它没有运行。请告诉我解决办法_Java_Android_Xml_Sqlite_Sqliteopenhelper - Fatal编程技术网

Java 我为本地sqlite数据库创建了应用程序,但它没有运行。请告诉我解决办法

Java 我为本地sqlite数据库创建了应用程序,但它没有运行。请告诉我解决办法,java,android,xml,sqlite,sqliteopenhelper,Java,Android,Xml,Sqlite,Sqliteopenhelper,这是DatabaseHelper.java类,在这个类中我为SQLite数据库编写查询。我认为这个问题不存在 package com.example.ankurnamikaze.myapp; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLit

这是DatabaseHelper.java类,在这个类中我为SQLite数据库编写查询。我认为这个问题不存在

package com.example.ankurnamikaze.myapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static android.R.attr.id;
import static android.R.attr.name;

public class DatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase db;
public static final String DATABASE_NAME = "notes_db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "notes";
public static final String COL_ID = "id";
public static final String COL_NAME = "name";
public static final String COL_PASS = "pass";
此方法用于创建数据库

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (" +COL_ID + " integer autoincrement," + COL_NAME + " text," + COL_PASS+ " text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists " + TABLE_NAME);
    onCreate(db);
}

// INSERTING ROW IN DATABASE

public void insertData(int id, String name, String pass){
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COL_ID, id);
    values.put(COL_NAME, name);
    values.put(COL_PASS, pass);
    db.insert(TABLE_NAME, null, values);
}
}
这是MainActivity.java类。我想我在定义DatabaseHelper类时遇到了问题,因为以前我在onCreate方法外定义它时,它会使应用程序崩溃,然后我看到一些代码,它们告诉我在onCreate方法内定义它,但它仍然不适用于我

package com.example.ankurnamikaze.myapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import static android.R.attr.id;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;

public class MainActivity extends AppCompatActivity {
DatabaseHelper databaseHelper;
EditText name, password;
String user_name, user_pass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    databaseHelper = new DatabaseHelper(this);
    setContentView(R.layout.activity_main);
    name = (EditText)findViewById(R.id.user_name);
    password = (EditText)findViewById(R.id.user_pass);
    user_name = String.valueOf(name.getText());
    user_pass = String.valueOf(password.getText());
    databaseHelper.insertData(id, user_name, user_pass);
}
}
这是activiy_main.xml,在这里我创建了属性。我不认为这是问题的根源

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ankurnamikaze.myapp.MainActivity">


<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="USERNAME"
android:id="@+id/user_name"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="PASSWORD"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="sumbit"
    android:id="@+id/button" />

</LinearLayout>

存在一些错误/问题

问题1 您已将
编码为“integer autoincrement”,
这将导致语法错误,因为autoincrement关键字只能与
integer主键一起使用(表示不建议使用它,因为integer主键效率更高,且具有非常相同的结果,即自动生成唯一id)(实际上,在这两种情况下,它都只使id成为rowid的别名)

e、 g

AUTOINCREMENT关键字会增加CPU、内存、磁盘空间和内存 磁盘I/O开销,如果不是严格需要的话,应该避免 通常不需要

该错误将导致以下情况:-

09-19 07:32:10.040 7200-7200/? E/SQLiteLog: (1) near "autoincrement": syntax error
09-19 07:32:10.040 7200-7200/? D/AndroidRuntime: Shutting down VM
09-19 07:32:10.040 7200-7200/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa62e9288)
09-19 07:32:10.044 7200-7200/? E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{so52399661.so52399661/so52399661.so52399661.MainActivity}: android.database.sqlite.SQLiteException: near "autoincrement": syntax error (code 1): , while compiling: create table notes (id integer autoincrement,name text,pass text)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.database.sqlite.SQLiteException: near "autoincrement": syntax error (code 1): , while compiling: create table notes (id integer autoincrement,name text,pass text)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
        at so52399661.so52399661.DatabaseHelper.onCreate(DatabaseHelper.java:23)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
        at so52399661.so52399661.DatabaseHelper.insertData(DatabaseHelper.java:38)
        at so52399661.so52399661.MainActivity.onCreate(MainActivity.java:24)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
        at android.app.ActivityThread.access$600(ActivityThread.java:130) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:137) 
        at android.app.ActivityThread.main(ActivityThread.java:4745) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
        at dalvik.system.NativeStart.main(Native Method) 
问题2 布局中没有定义id用户通行证

问题3 无法解析id(请参见下文,这是不需要的)

解决方案 以下是固定代码(请参阅注释),其中包含一些用于列出数据库中的表的附加代码:-

activity\u main.xml(id user\u pass添加到密码编辑文本中):-


i、 e.您notes表存在,android\u metedata是由您的DatabaseHelper是其子类的SQLiteOpenhelper类创建的表。它包含区域设置。

您的问题是什么?您得到了什么错误?它到底是如何不运行的?它不编译、崩溃还是什么?我应该添加andr给出的编译行吗oid studio?非常感谢,我还有一个问题,sqlite是否支持NOT null,因为我认为它与SQL中的工作方式不同,如果用户没有输入数据就离开了任何列,您还可以向我建议什么。我希望您理解我想说的。@FootstaR
db.execSQL(“创建表”+表名+”(“+COL\u ID+“整型主键,“+COL_NAME+”TEXT NOT NULL,“+COL_PASS+”TEXT NOT NULL)”;
如果NAME或PASS为NULL,将导致冲突。但是,我建议检查user_PASS.getText().toString()和user_NAME.getText()返回的字符串的长度。如果两者的长度都小于1,则返回Toast消息。例如,如果((user_name.getText().toString()).length<1 | |(user_pass.getText().toString.length<1){…dotoast…}否则{…执行insert…}@FootstaR注释如果更改结构(例如添加notnull),则需要执行删除应用程序数据或卸载应用程序之类的操作(这是因为数据库助手的onCreate方法仅在创建数据库时运行。)@FootstaR最后,如果答案回答了您的问题,则您应该单击勾号以指示问题已被回答。
db.execSQL("create table " + TABLE_NAME + " (" +COL_ID + " INTEGER PRIMARY KEY," + COL_NAME + " TEXT," + COL_PASS+ " TEXT)");
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="USERNAME"
        android:id="@+id/user_name"/>

    <EditText
        android:id="@+id/user_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="PASSWORD"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="sumbit"
        android:id="@+id/button" />

</LinearLayout>
public class DatabaseHelper extends SQLiteOpenHelper {
    SQLiteDatabase db;
    public static final String DATABASE_NAME = "notes_db";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_NAME = "notes";
    public static final String COL_ID = "id";
    public static final String COL_NAME = "name";
    public static final String COL_PASS = "pass";

    public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

    @Override
    public void onCreate(SQLiteDatabase db) {
        //db.execSQL("create table " + TABLE_NAME + " (" +COL_ID + " integer autoincrement," + COL_NAME + " text," + COL_PASS+ " text)"); //<<<<<<<<<< ERROR
        db.execSQL("create table " + TABLE_NAME + " (" +COL_ID + " INTEGER PRIMARY KEY," + COL_NAME + " TEXT," + COL_PASS+ " TEXT)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists " + TABLE_NAME);
        onCreate(db);
    }

// INSERTING ROW IN DATABASE

    //<<<<<<<<<< as INTEGER PRIMARY KEY then id will be automatically generated
    public void insertData(String name, String pass){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        //values.put(COL_ID, id); not needed will autogenerate
        values.put(COL_NAME, name);
        values.put(COL_PASS, pass);
        db.insert(TABLE_NAME, null, values);
    }
}
public class MainActivity extends AppCompatActivity {
    DatabaseHelper databaseHelper;
    EditText name, password;
    String user_name, user_pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        databaseHelper = new DatabaseHelper(this);
        setContentView(R.layout.activity_main);
        name = (EditText)findViewById(R.id.user_name);
        password = (EditText)findViewById(R.id.user_pass);
        user_name = String.valueOf(name.getText());
        user_pass = String.valueOf(password.getText());
        //databaseHelper.insertData(id, user_name, user_pass); //<<<<<<<<<< got rid of id
        databaseHelper.insertData(user_name, user_pass);

        //<<<<<<<<<< ADDED to List the tables
        Cursor csr = databaseHelper.getWritableDatabase().query("sqlite_master",null,null,null,null,null,null );
        while (csr.moveToNext()) {
            Log.d("TABLES","Found table " + csr.getString(csr.getColumnIndex("name")));
        }
        csr.close();
    }
}
09-19 07:10:20.628 6977-6977/so52399661.so52399661 D/TABLES: Found table android_metadata
    Found table notes