Overview

For maximum control over local data, developers can use SQLite directly by leveraging SQLiteOpenHelper for executing SQL requests and managing a local database.

In this guide, we'll use the example of building a database to persist user created "Posts" to demonstrate SQLite and SQLiteOpenHelper.

If you want to use SQLite directly but reduce the verbosity of working with the database, check out our Easier SQL with Cupboardguide for a middle ground between SQLite and a full-fledged ORM.

Defining the Database Handler

We need to write our own class to handle database operations such as creation, upgrading, reading and writing. Database operations are defined using the SQLiteOpenHelper:

public class PostsDatabaseHelper extends SQLiteOpenHelper {
// Database Info
private static final String DATABASE_NAME = "postsDatabase";
private static final int DATABASE_VERSION = 1; // Table Names
private static final String TABLE_POSTS = "posts";
private static final String TABLE_USERS = "users"; // Post Table Columns
private static final String KEY_POST_ID = "id";
private static final String KEY_POST_USER_ID_FK = "userId";
private static final String KEY_POST_TEXT = "text"; // User Table Columns
private static final String KEY_USER_ID = "id";
private static final String KEY_USER_NAME = "userName";
private static final String KEY_USER_PROFILE_PICTURE_URL = "profilePictureUrl"; public PostsDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
} // Called when the database connection is being configured.
// Configure database settings for things like foreign key support, write-ahead logging, etc.
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
} // Called when the database is created for the FIRST time.
// If a database already exists on disk with the same DATABASE_NAME, this method will NOT be called.
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_POSTS_TABLE = "CREATE TABLE " + TABLE_POSTS +
"(" +
KEY_POST_ID + " INTEGER PRIMARY KEY," + // Define a primary key
KEY_POST_USER_ID_FK + " INTEGER REFERENCES " + TABLE_USERS + "," + // Define a foreign key
KEY_POST_TEXT + " TEXT" +
")"; String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS +
"(" +
KEY_USER_ID + " INTEGER PRIMARY KEY," +
KEY_USER_NAME + " TEXT," +
KEY_USER_PROFILE_PICTURE_URL + " TEXT" +
")"; db.execSQL(CREATE_POSTS_TABLE);
db.execSQL(CREATE_USERS_TABLE);
} // Called when the database needs to be upgraded.
// This method will only be called if a database already exists on disk with the same DATABASE_NAME,
// but the DATABASE_VERSION is different than the version of the database that exists on disk.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion != newVersion) {
// Simplest implementation is to drop all old tables and recreate them
db.execSQL("DROP TABLE IF EXISTS " + TABLE_POSTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
onCreate(db);
}
}
}

Important Note: The SQLite database is lazily initialized. This means that it isn't actually created until it's first accessed through a call to getReadableDatabase() or getWriteableDatabase(). This also means that any methods that callgetReadableDatabase() or getWriteableDatabase() should be done on a background thread as there is a possibility that they might be kicking off the initial creation of the database.

Singleton Pattern

Often a SQLite database will be used across your entire application; within services, applications, fragments, and more. For this reason, best practices often advise you to apply the singleton pattern to your SQLiteOpenHelper instances to avoid memory leaks and unnecessary reallocations. The best solution is to make your database instance a singleton instance across the entire application's lifecycle.

public class PostsDatabaseHelper extends SQLiteOpenHelper {
private static PostsDatabaseHelper sInstance; // ... public static synchronized PostsDatabaseHelper getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (sInstance == null) {
sInstance = new PostsDatabaseHelper(context.getApplicationContext());
}
return sInstance;
} /**
* Constructor should be private to prevent direct instantiation.
* Make a call to the static method "getInstance()" instead.
*/
private PostsDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}

The static getInstance() method ensures that only one PostsDatabaseHelper will ever exist at any given time. If thesInstance object has not been initialized, one will be created. If one has already been created then it will simply be returned. Then we can access our database connection with:

// In any activity just pass the context and use the singleton method
PostsDatabaseHelper helper = PostsDatabaseHelper.getInstance(this);

See this android design patterns article for more information.

Defining our Models

In order to access our records from the database more easily, we should create a model class for each of our resources. In this case, let's define a Post and a User model:

public class Post {
public User user;
public String text;
}
public class User {
public String userName;
public String profilePictureUrl;
}

Now we can interact with our data using the models.

CRUD Operations (Create, Read, Update, Delete)

We'll walk through examples of creating, reading, updating, and deleting posts / users in our database.

Inserting New Records

public class PostsDatabaseHelper extends SQLiteOpenHelper {
// ...existing methods... // Insert a post into the database
public void addPost(Post post) {
// Create and/or open the database for writing
SQLiteDatabase db = getWritableDatabase(); // It's a good idea to wrap our insert in a transaction. This helps with performance and ensures
// consistency of the database.
db.beginTransaction();
try {
// The user might already exist in the database (i.e. the same user created multiple posts).
long userId = addOrUpdateUser(post.user); ContentValues values = new ContentValues();
values.put(KEY_POST_USER_ID_FK, userId);
values.put(KEY_POST_TEXT, post.text); // Notice how we haven't specified the primary key. SQLite auto increments the primary key column.
db.insertOrThrow(TABLE_POSTS, null, values);
db.setTransactionSuccessful();
} catch (Exception e) {
Log.d(TAG, "Error while trying to add post to database");
} finally {
db.endTransaction();
}
} // Insert or update a user in the database
// Since SQLite doesn't support "upsert" we need to fall back on an attempt to UPDATE (in case the
// user already exists) optionally followed by an INSERT (in case the user does not already exist).
// Unfortunately, there is a bug with the insertOnConflict method
// (https://code.google.com/p/android/issues/detail?id=13045) so we need to fall back to the more
// verbose option of querying for the user's primary key if we did an update.
public long addOrUpdateUser(User user) {
// The database connection is cached so it's not expensive to call getWriteableDatabase() multiple times.
SQLiteDatabase db = getWritableDatabase();
long userId = -1; db.beginTransaction();
try {
ContentValues values = new ContentValues();
values.put(KEY_USER_NAME, user.userName);
values.put(KEY_USER_PROFILE_PICTURE_URL, user.profilePictureUrl); // First try to update the user in case the user already exists in the database
// This assumes userNames are unique
int rows = db.update(TABLE_USERS, values, KEY_USER_NAME + "= ?", new String[]{user.userName}); // Check if update succeeded
if (rows == 1) {
// Get the primary key of the user we just updated
String usersSelectQuery = String.format("SELECT %s FROM %s WHERE %s = ?",
KEY_USER_ID, TABLE_USERS, KEY_USER_NAME);
Cursor cursor = db.rawQuery(usersSelectQuery, new String[]{String.valueOf(user.userName)});
try {
if (cursor.moveToFirst()) {
userId = cursor.getInt(0);
db.setTransactionSuccessful();
}
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
} else {
// user with this userName did not already exist, so insert new user
userId = db.insertOrThrow(TABLE_USERS, null, values);
db.setTransactionSuccessful();
}
} catch (Exception e) {
Log.d(TAG, "Error while trying to add or update user");
} finally {
db.endTransaction();
}
return userId;
}
}

Note: If you are inserting a large number of records, you might want to use a compiled SQLiteStatement. You can read more about the performance benefits on this blog.

Querying Records

public class PostsDatabaseHelper extends SQLiteOpenHelper {
// ...existing methods... public List<Post> getAllPosts() {
List<Post> posts = new ArrayList<>(); // SELECT * FROM POSTS
// LEFT OUTER JOIN USERS
// ON POSTS.KEY_POST_USER_ID_FK = USERS.KEY_USER_ID
String POSTS_SELECT_QUERY =
String.format("SELECT * FROM %s LEFT OUTER JOIN %s ON %s.%s = %s.%s",
TABLE_POSTS,
TABLE_USERS,
TABLE_POSTS, KEY_POST_USER_ID_FK,
TABLE_USERS, KEY_USER_ID); // "getReadableDatabase()" and "getWriteableDatabase()" return the same object (except under low
// disk space scenarios)
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(POSTS_SELECT_QUERY, null);
try {
if (cursor.moveToFirst()) {
do {
User newUser = new User();
newUser.userName = cursor.getString(cursor.getColumnIndex(KEY_USER_NAME));
newUser.profilePictureUrl = cursor.getString(cursor.getColumnIndex(KEY_USER_PROFILE_PICTURE_URL)); Post newPost = new Post();
newPost.text = cursor.getString(cursor.getColumnIndex(KEY_POST_TEXT));
newPost.user = newUser;
posts.add(newPost);
} while(cursor.moveToNext());
}
} catch (Exception e) {
Log.d(TAG, "Error while trying to get posts from database");
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return posts;
}
}

Updating Records

public class PostsDatabaseHelper extends SQLiteOpenHelper {
// ...existing methods... // Update the user's profile picture url
public int updateUserProfilePicture(User user) {
SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues();
values.put(KEY_USER_PROFILE_PICTURE_URL, user.profilePictureUrl); // Updating profile picture url for user with that userName
return db.update(TABLE_USERS, values, KEY_USER_NAME + " = ?",
new String[] { String.valueOf(user.userName) });
}
}

Deleting Records

public class PostsDatabaseHelper extends SQLiteOpenHelper {
// ...existing methods... public void deleteAllPostsAndUsers() {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
// Order of deletions is important when foreign key relationships exist.
db.delete(TABLE_POSTS, null, null);
db.delete(TABLE_USERS, null, null);
db.setTransactionSuccessful();
} catch (Exception e) {
Log.d(TAG, "Error while trying to delete all posts and users");
} finally {
db.endTransaction();
}
}
}

Using our Database Handler

We can now leverage our database handler and models to persist data to our SQLite store:

public class SQLiteExampleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite_example); // Create sample data
User sampleUser = new User();
sampleUser.userName = "Steph";
sampleUser.profilePictureUrl = "https://i.imgur.com/tGbaZCY.jpg"; Post samplePost = new Post();
samplePost.user = sampleUser;
samplePost.text = "Won won!"; // Get singleton instance of database
PostsDatabaseHelper databaseHelper = PostsDatabaseHelper.getInstance(this); // Add sample post to the database
databaseHelper.addPost(samplePost); // Get all posts from database
List<Post> posts = databaseHelper.getAllPosts();
for (Post post : posts) {
// do something
}
}
}

Note: In many cases, rather than interacting with SQL directly, Android apps can leverage one of the many available higher-level ORMs (object relational mappers) to persist Java models to a database table instead.

Note: In many cases, rather than interacting with SQL directly, Android apps can leverage one of the many available higher-level ORMs (object relational mappers) to persist Java models to a database table instead.

Full Database Handler Source

The full source code for the database handler above can be found here for reference:

public class PostsDatabaseHelper extends SQLiteOpenHelper {
// Database Info
private static final String DATABASE_NAME = "postsDatabase";
private static final int DATABASE_VERSION = 1; // Table Names
private static final String TABLE_POSTS = "posts";
private static final String TABLE_USERS = "users"; // Post Table Columns
private static final String KEY_POST_ID = "id";
private static final String KEY_POST_USER_ID_FK = "userId";
private static final String KEY_POST_TEXT = "text"; // User Table Columns
private static final String KEY_USER_ID = "id";
private static final String KEY_USER_NAME = "userName";
private static final String KEY_USER_PROFILE_PICTURE_URL = "profilePictureUrl"; private static PostsDatabaseHelper sInstance; public static synchronized PostsDatabaseHelper getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (sInstance == null) {
sInstance = new PostsDatabaseHelper(context.getApplicationContext());
}
return sInstance;
} /**
* Constructor should be private to prevent direct instantiation.
* Make a call to the static method "getInstance()" instead.
*/
private PostsDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
} // Called when the database connection is being configured.
// Configure database settings for things like foreign key support, write-ahead logging, etc.
@Override
public void onConfigure(SQLiteDatabase db) {
super.onConfigure(db);
db.setForeignKeyConstraintsEnabled(true);
} // Called when the database is created for the FIRST time.
// If a database already exists on disk with the same DATABASE_NAME, this method will NOT be called.
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_POSTS_TABLE = "CREATE TABLE " + TABLE_POSTS +
"(" +
KEY_POST_ID + " INTEGER PRIMARY KEY," + // Define a primary key
KEY_POST_USER_ID_FK + " INTEGER REFERENCES " + TABLE_USERS + "," + // Define a foreign key
KEY_POST_TEXT + " TEXT" +
")"; String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS +
"(" +
KEY_USER_ID + " INTEGER PRIMARY KEY," +
KEY_USER_NAME + " TEXT," +
KEY_USER_PROFILE_PICTURE_URL + " TEXT" +
")"; db.execSQL(CREATE_POSTS_TABLE);
db.execSQL(CREATE_USERS_TABLE);
} // Called when the database needs to be upgraded.
// This method will only be called if a database already exists on disk with the same DATABASE_NAME,
// but the DATABASE_VERSION is different than the version of the database that exists on disk.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion != newVersion) {
// Simplest implementation is to drop all old tables and recreate them
db.execSQL("DROP TABLE IF EXISTS " + TABLE_POSTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
onCreate(db);
}
} // Insert a post into the database
public void addPost(Post post) {
// Create and/or open the database for writing
SQLiteDatabase db = getWritableDatabase(); // It's a good idea to wrap our insert in a transaction. This helps with performance and ensures
// consistency of the database.
db.beginTransaction();
try {
// The user might already exist in the database (i.e. the same user created multiple posts).
long userId = addOrUpdateUser(post.user); ContentValues values = new ContentValues();
values.put(KEY_POST_USER_ID_FK, userId);
values.put(KEY_POST_TEXT, post.text); // Notice how we haven't specified the primary key. SQLite auto increments the primary key column.
db.insertOrThrow(TABLE_POSTS, null, values);
db.setTransactionSuccessful();
} catch (Exception e) {
Log.d(TAG, "Error while trying to add post to database");
} finally {
db.endTransaction();
}
} // Insert or update a user in the database
// Since SQLite doesn't support "upsert" we need to fall back on an attempt to UPDATE (in case the
// user already exists) optionally followed by an INSERT (in case the user does not already exist).
// Unfortunately, there is a bug with the insertOnConflict method
// (https://code.google.com/p/android/issues/detail?id=13045) so we need to fall back to the more
// verbose option of querying for the user's primary key if we did an update.
public long addOrUpdateUser(User user) {
// The database connection is cached so it's not expensive to call getWriteableDatabase() multiple times.
SQLiteDatabase db = getWritableDatabase();
long userId = -1; db.beginTransaction();
try {
ContentValues values = new ContentValues();
values.put(KEY_USER_NAME, user.userName);
values.put(KEY_USER_PROFILE_PICTURE_URL, user.profilePictureUrl); // First try to update the user in case the user already exists in the database
// This assumes userNames are unique
int rows = db.update(TABLE_USERS, values, KEY_USER_NAME + "= ?", new String[]{user.userName}); // Check if update succeeded
if (rows == 1) {
// Get the primary key of the user we just updated
String usersSelectQuery = String.format("SELECT %s FROM %s WHERE %s = ?",
KEY_USER_ID, TABLE_USERS, KEY_USER_NAME);
Cursor cursor = db.rawQuery(usersSelectQuery, new String[]{String.valueOf(user.userName)});
try {
if (cursor.moveToFirst()) {
userId = cursor.getInt(0);
db.setTransactionSuccessful();
}
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
} else {
// user with this userName did not already exist, so insert new user
userId = db.insertOrThrow(TABLE_USERS, null, values);
db.setTransactionSuccessful();
}
} catch (Exception e) {
Log.d(TAG, "Error while trying to add or update user");
} finally {
db.endTransaction();
}
return userId;
} // Get all posts in the database
public List<Post> getAllPosts() {
List<Post> posts = new ArrayList<>(); // SELECT * FROM POSTS
// LEFT OUTER JOIN USERS
// ON POSTS.KEY_POST_USER_ID_FK = USERS.KEY_USER_ID
String POSTS_SELECT_QUERY =
String.format("SELECT * FROM %s LEFT OUTER JOIN %s ON %s.%s = %s.%s",
TABLE_POSTS,
TABLE_USERS,
TABLE_POSTS, KEY_POST_USER_ID_FK,
TABLE_USERS, KEY_USER_ID); // "getReadableDatabase()" and "getWriteableDatabase()" return the same object (except under low
// disk space scenarios)
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery(POSTS_SELECT_QUERY, null);
try {
if (cursor.moveToFirst()) {
do {
User newUser = new User();
newUser.userName = cursor.getString(cursor.getColumnIndex(KEY_USER_NAME));
newUser.profilePictureUrl = cursor.getString(cursor.getColumnIndex(KEY_USER_PROFILE_PICTURE_URL)); Post newPost = new Post();
newPost.text = cursor.getString(cursor.getColumnIndex(KEY_POST_TEXT));
newPost.user = newUser;
posts.add(newPost);
} while(cursor.moveToNext());
}
} catch (Exception e) {
Log.d(TAG, "Error while trying to get posts from database");
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return posts;
} // Update the user's profile picture url
public int updateUserProfilePicture(User user) {
SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues();
values.put(KEY_USER_PROFILE_PICTURE_URL, user.profilePictureUrl); // Updating profile picture url for user with that userName
return db.update(TABLE_USERS, values, KEY_USER_NAME + " = ?",
new String[] { String.valueOf(user.userName) });
} // Delete all posts and users in the database
public void deleteAllPostsAndUsers() {
SQLiteDatabase db = getWritableDatabase();
db.beginTransaction();
try {
// Order of deletions is important when foreign key relationships exist.
db.delete(TABLE_POSTS, null, null);
db.delete(TABLE_USERS, null, null);
db.setTransactionSuccessful();
} catch (Exception e) {
Log.d(TAG, "Error while trying to delete all posts and users");
} finally {
db.endTransaction();
}
}
}

SQLite Database Debugging

When working with SQLite, opening and inspecting the SQLite database can be helpful while debugging issues. You can leverage the Stetho library to view your data directly, or you can use the following command-line tools to retrieve the data.

The commands below will show how to get at the data (whether running on an emulator or an actual device). The commands should be performed within the terminal or command-line. Once you have the data, there are desktop SQLite viewers to help inspect the SQLite data graphically.

On an Emulator

Use SQLite3 to query the data on the emulator:

cd /path/to/my/sdk/platform-tools
./adb shell
run-as <app package name>
cd /data/data/<app package name>/databases
ls
chmod 666 <database file name>
sqlite3 <database file name>
> (semi-colon terminated commands can be run here to query the data)
> .exit
(copy full database path)
exit

For further inspection, we can download the database file with:

./adb wait-for-device pull /data/data/<app package name>/databases/<database file name>

On a Device

There isn't a SQLite3 executable on the device so our only option is to download the database file with:

./adb shell run-as <app package name> chmod 666 /data/data/<app package name>/databases/<database file name>
./adb shell cp /data/data/<app package name>/databases/<database file name> /sdcard/
./adb pull /sdcard/<database file name>

References

Local Databases with SQLiteOpenHelper的更多相关文章

  1. Persisting Data to the Device

    Persisting Data to the Device Edit PagePage History Overview The Android framework offers several op ...

  2. MongoDB 安全和访问权限控制

    MongoDB的访问控制能够有效保证数据库的安全,访问控制是指绑定Application监听的IP地址,设置监听端口,使用账户和密码登录 一,访问控制的参数 1,绑定IP地址 mongod 参数:-- ...

  3. MongoDB的内置角色 Built-In Roles

    关于芒果的权限控制说白了就是定义 Role(角色) 来控制对数据库进行的操作(调用的方法比如查询方法find). 系统内置的Role分为 以下几大类: Database User Roles 这个是针 ...

  4. pouchdb Conflicts

    Conflicts are an unavoidable reality when dealing with distributed systems. And make no mistake: cli ...

  5. pouchdb sync

    PouchDB and CouchDB were designed for one main purpose: sync. Jason Smith has a great quote about th ...

  6. DB2调优摘要

    感谢原博主 http://yishueitian326.blog.163.com/blog/static/2858637520106695733719/ 性 能是关系到随需应变型应用程序成功与否的关键 ...

  7. mongoDB基础使用

    环境交代 操作系统: CentOS 6.8 64位 mongodb: 4.06 安装 官方下载地址:https://www.mongodb.org/dl/linux/x86_64-rhel62 阿里云 ...

  8. MongoDB 设置账号和密码

    一.安装MongoDB 1.环境配置: i.操作系统:CentOS release 6.8 (Final) [root@iZ2ze2pbbffhmn53ao4tuaZ bin]# cat /etc/r ...

  9. (转)调优 DB2 UDB v8.1 及其数据库的最佳实践

    原文:https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0404mcarthur/index.html 简介 性能是 ...

随机推荐

  1. 理解js的几个关键问题(1):全局变量new和关于hasOwnPropery和PropertyIsEnumerable 等

    一.作用域和全局变量 var test=function(){ var a=1; setTimeout(function(){ console.log(a); a=2; },1000); a=3; s ...

  2. Python中input()和raw_input()函数的区别

    问题:在Python2.7中使用 input() 函数会出现 “NameError: Name ”***“ is not defined 的错误 解决: 使用raw_input() 函数,在Pytho ...

  3. bzoj 1702 贪心,前缀和

    [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 807  Solved: ...

  4. uva12558 Egyptian Fractions (HARD version)(迭代深搜)

    Egyptian Fractions (HARD version) 题解:迭代深搜模板题,因为最小个数,以此为乐观估价函数来迭代深搜,就可以了. #include<cstdio> #inc ...

  5. CPU问题定位与解决

    CPU问题定位基本流程:   性能计数器诊断 主要用到的性能计数器 %Process Time 全实例 (主要用于查看当前服务器的CPU 情况) %Process Time sqlservr (主要用 ...

  6. [NOIP2000] 提高组 洛谷P1019 单词接龙

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...

  7. 【搜索引擎】SOLR VS Elasticsearch(2019技术选型参考)

    SOLR是什么 (官方的解释) Solr是基于Apache Lucene构建的流行的.快速的.开源的企业搜索平台. Solr也是高度可靠.可伸缩和容错的,提供分布式索引.复制和负载平衡查询.自动故障转 ...

  8. Tomcat错误信息(服务器版本号)泄露(低危)

    一.问题描述Tomcat报错页面泄漏Apache Tomcat/7.0.52相关版本号信息,是攻击者攻击的途径之一.因此实际当中建议去掉版本号信息. 二.解决办法 1.进入到tomcat/lib目录下 ...

  9. 【深度探索c++对象模型】Function语义学之成员函数调用方式

    非静态成员函数 c++的设计准则之一就是:非静态成员函数至少和一般的非成员函数有相同的效率.编译器内部已将member函数实体转换为对等的nonmember函数实体. 转化步骤: 1.改写函数原型以安 ...

  10. Could not find leader nimbus

    运行storm ui, 然后访问storm ui 的网页的时候,死活跑不起来.后面,根据下面这篇文章的说法, 停止zookeeper 之后,删掉zookeeper 上面的storm 节点, 然后再重启 ...